Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在文本文件中查找整数在新文本文件中重写为字符串Python_Python - Fatal编程技术网

在文本文件中查找整数在新文本文件中重写为字符串Python

在文本文件中查找整数在新文本文件中重写为字符串Python,python,Python,我正在尝试编写一个函数,逐行查看文本文件,其中包含诸如“2+6=8”之类的字符串。我希望这个程序遍历文本文件,如果它找到一个整数,它会将其更改为整数的拼写名称 所以在这个例子中,它打开文件,读取它,看到2+6=8,并将其更改为2+6=8 有人能帮我吗 谢谢如果您有超过9的数字,这将很困难,但如果没有 from_ = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] to = ['zero','one','two','three','four',

我正在尝试编写一个函数,逐行查看文本文件,其中包含诸如“2+6=8”之类的字符串。我希望这个程序遍历文本文件,如果它找到一个整数,它会将其更改为整数的拼写名称

所以在这个例子中,它打开文件,读取它,看到2+6=8,并将其更改为2+6=8

有人能帮我吗


谢谢

如果您有超过
9
的数字,这将很困难,但如果没有

from_ = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
to = ['zero','one','two','three','four','five','six','seven','eight','nine']
table = str.maketrans(dict( zip(from_, to) ))

line = "2 plus 6 = 8"
output = line.translate(table)
# output == "two plus six = eight"
您可以通过执行以下操作来构建它以查看文件:

def spellnumbers(line):
    from_ = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    to = ['zero','one','two','three','four','five','six','seven','eight','nine']
    table = str.maketrans( dict(zip(from_, to)) )
    return line.translate(table)

with open('path/to/input/file.txt') as inf and open('path/to/output/file.txt', 'w') as outf:
    for line in inf:
        outf.write(spellnumbers(line))
        outf.write('\n')
这基本上只是建立一个字典的形式:

{ "0": "zero", "1": "one", ... , "9": "nine" }
然后从中构建一个翻译表,并在翻译过程中运行字符串

如果您的数字超过了
9
,那么您将遇到一个问题
“10+2=12”
变为
“一零+二=一二”
这个问题是不寻常的

编辑 我碰巧看到你的回复提到你不允许使用“表格或字典”,这是一种愚蠢的要求,但没关系。如果这是真的,那么这一定是学校的作业,在这种情况下,我不会为你做家庭作业,但可能会指引你正确的方向:

def spellnumbers(line):
    # 1. split the line into words. Remember str.split
    # 2. create an empty string that you can write output to. We'll
    #    use that more in a minute.
    # 3. iterate through those words with a for loop and check if
    #    word == '1':, elif word == '2'; elif word == '3', etc
    # 4. for each if block, add the number's name ("one", "two") to
    #    the output string we created in step 2
    # 5. you'll want an else block that just writes the word to
    #    the output string
    # 6. return the output string

f = open('path/to/file.txt')
lines = f.readlines() # this is ugly, but you're a beginner so we'll stick with this
for line in lines:
    stripped_line = line.strip() # removes leading and trailing whitespace e.g. \n
    print(spellnumbers(line))
    # are you printing the output? How are you displaying it?

您可以使用此代码的倒数作为9以外的数字的基数:。这将是一个复制和粘贴,然后切换键和值。一个带有
(\d+)
的正则表达式可以用来一起查找数字。@SethMMorton不久前我确实写了一些东西来做这件事,基本上是因为我在工作的待命时间里感到无聊。就在这里@SethMMorton我觉得我在这里被利用@AdamSmith也许我们应该用愚蠢的限制来包装糟糕的家庭作业的解决方案!:P@AdamSmith也许当我有更多的时间时,我可以把它改写成英文。