已解决如何将数字单词转换为 phone 数字初学者风格?

SOLVED How do I convert numeric words to a phone numbers beginner style?

问题:

我是 Python 编程的初学者(3 个月),我的学校作业要求我获取一个包含数字单词(例如:三哦五九...)的文本文件并将其转换到 phone 个数字(见下面的列表)在一个单独的文件中。我一直在绞尽脑汁,找不到一种简单的、对初学者友好的风格来为此编写代码。我将不胜感激。

我的IPO计划如下:

输入: 以读取模式打开文本文件。

处理中: 将文本文件拆分为列表中的单词。 将每个单词转换为相应的数字。 显示字符串中的每个数字。

输出: 打印包含 phone 数字的字符串。

我似乎无法将我的计划转换为代码。

输入是一个如下所示的文件:

*编辑:问题已解决,这是最终(有效)代码:

di = {"oh":0,"one":1,"two":2,"three":3,"four":4,"five":5,"six":6,"seven":7,"eight":8,"nine":9}
c=""

infile=open("digit_words.txt","r")
outfile=open("digit_strings.txt","w")

l=infile.readlines()

for line in l:
    words=line.split()

    for nos in words:
        c+=str(di[nos])

    print(c, file=outfile)
    print("\n")
    c=""

infile.close()
outfile.close()}

匹配输出是另一个文件,如下所示:

好的,让我们将您的计划更改为代码。假设文件名为 phno.txt,您的代码将是:

di= {"zero":0,"one":1,"two":2,"three":3,"four":4,"five":5,"six":6,"seven":7,"eight":8,"nine":9}
c=""
f=open("phno.txt","r")
l=f.readlines()
for line in l:
    words=line.split()
    for nos in words:
         c+=str(di[nos])
    print(c)
    print("\n")
    c=""