来自sys.stdin的单词的数字

时间:2016-10-15 18:35:57

标签: python words digits

我正在尝试将数字转换为std输入(txt文件)中的单词。 如果输入是例如:1234,我希望输出为一个二三四,对于文本文件中的下一行,我希望输出在shell /终端的新行上: 1234一二三四 56五六 问题是我不能让它在同一条线上输出。 代码到目前为止:

#!/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()

1 个答案:

答案 0 :(得分:1)

将单词放入列表中,加入它们,然后打印该行。

#!/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()