sys.stdin 中的数字到单词
digits to words from sys.stdin
我正在尝试将标准输入(txt 文件)中的数字转换为单词。
例如,如果输入为:1234,我希望输出为一二三四,对于文本文件中的下一行,我希望输出位于 shell/terminal 中的新行:
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()
将单词放入列表中,加入它们,然后打印行。
#!/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()
我正在尝试将标准输入(txt 文件)中的数字转换为单词。 例如,如果输入为:1234,我希望输出为一二三四,对于文本文件中的下一行,我希望输出位于 shell/terminal 中的新行: 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()
将单词放入列表中,加入它们,然后打印行。
#!/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()