stefan lakin stefan lakin - 1 month ago 7
Python Question

digits to words from sys.stdin

I'm trying to convert digits to words from std input (txt file).
If the input is for example : 1234, i want the output to be one two three four, for the next line in the text file i want the output to be on a new line in the shell/terminal:
1234 one two three four
56 five six
The problem is that i can't get it to output on the same line.
Code so far :

#!/usr/bin/python3

import sys
import math

def main():
number_list = ["zero","one","two","three","four","five","six","seven","eight","nine"]
for line in sys.stdin:
number = line.split()

for i in number:
number_string = "".join(i)
number2 = int(number_string)
print(number_list[number2])
main()

xli xli
Answer

Put the words in a list, join them, and print the line.

#!/usr/bin/python3
import sys
import math

def main():
        number_list = ["zero","one","two","three","four","five","six","seven","eight","nine"]
        for line in sys.stdin:
                digits = list(line.strip())
                words =  [number_list[int(digit)] for digit in digits]
                words_line = ' '.join(words)
                print(words_line)
main()