Python2.7-使用字典从文本文件查找和替换到新的文本fi

2024-05-23 15:41:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我是编程新手,过去几个月我一直在业余时间学习python。我决定尝试在一个文本文件中创建一个小脚本,将美国拼法转换成英语拼法。

在过去的5个小时里,我一直在尝试各种各样的事情,但最终还是想出了一些能让我更接近目标的东西,但还没有达到目标!

#imported dictionary contains 1800 english:american spelling key:value pairs. 
from english_american_dictionary import dict


def replace_all(text, dict):
    for english, american in dict.iteritems():
        text = text.replace(american, english)
    return text


my_text = open('test_file.txt', 'r')

for line in my_text:
    new_line = replace_all(line, dict)
    output = open('output_test_file.txt', 'a')
    print >> output, new_line

output.close()

我相信有一个更好的方法来解决问题,但是对于这个脚本,我有以下问题:

  • 在输出文件中,每行写一行,中间有一个换行符,但是原始的test_file.txt没有这个。此页底部显示的test_file.txt的内容
  • 只有一行中的第一个美国拼法被转换成英语。
  • 我真的不想以追加模式打开输出文件,但在这个代码结构中找不到“r”。

感谢您的帮助!

test_file.txt的内容是:

I am sample file.
I contain an english spelling: colour.
3 american spellings on 1 line: color, analyze, utilize.
1 american spelling on 1 line: familiarize.

Tags: texttesttxt脚本foroutputdictionaryenglish
3条回答

print语句添加了自己的新行,但您的行已经有了自己的新行。您可以从new_line中删除换行符,或者使用较低级别

output.write(new_line)

取而代之的是(它准确地写出你传递给它的内容)。

关于第二个问题,我想我们需要一个实际的例子。replace()确实应该替换所有匹配项。

>>> "abc abc abcd ab".replace("abc", "def")
'def def defd ab'

我不知道你的第三个问题是什么。如果要替换输出文件,请执行

output = open('output_test_file.txt', 'w')

'w'表示您正在打开文件进行写入。

您看到的额外空行是因为您正在使用print写出一行,该行的末尾已经包含一个换行符。由于print也编写了自己的换行符,因此您的输出将变为双倍行距。一个简单的解决方法是使用outfile.write(new_line)代替。

至于文件模式,问题是您要反复打开输出文件。你应该一开始就打开一次。使用with语句来处理打开的文件通常是一个好主意,因为当您处理完它们时,它们会帮您关闭它们。

我不理解你的另一个问题,只有一些替代发生。你的字典缺少'analyze''utilize'的拼写吗?

我的一个建议是不要逐行更换。您可以使用file.read()一次读取整个文件,然后将其作为单个单元处理。这可能会更快,因为它不需要在拼写词典中的项目上循环那么频繁(只循环一次,而不是每行循环一次):

with open('test_file.txt', 'r') as in_file:
    text = in_file.read()

with open('output_test_file.txt', 'w') as out_file:
    out_file.write(replace_all(text, spelling_dict))

编辑:

为了使代码正确地处理包含其他单词的单词(比如包含“tire”的“entire”),您可能需要放弃简单的str.replace方法,转而使用正则表达式。

下面是一个使用re.sub的快速拼凑解决方案,给出了一个从美式英语到英式英语的拼写变化词典(即,与当前词典的顺序相反):

import re

#from english_american_dictionary import ame_to_bre_spellings
ame_to_bre_spellings = {'tire':'tyre', 'color':'colour', 'utilize':'utilise'}

def replacer_factory(spelling_dict):
    def replacer(match):
        word = match.group()
        return spelling_dict.get(word, word)
    return replacer

def ame_to_bre(text):
    pattern = r'\b\w+\b'  # this pattern matches whole words only
    replacer = replacer_factory(ame_to_bre_spellings)
    return re.sub(pattern, replacer, text)

def main():
    #with open('test_file.txt') as in_file:
    #    text = in_file.read()
    text = 'foo color, entire, utilize'

    #with open('output_test_file.txt', 'w') as out_file:
    #    out_file.write(ame_to_bre(text))
    print(ame_to_bre(text))

if __name__ == '__main__':
    main()

这种代码结构的一个好处是,如果您按照另一个顺序将字典传递给replacer_factory函数,那么您可以轻松地从英式英语拼写转换回美式英语拼写。

作为以上所有的好答案,我写了一个新版本,我认为是更Python,希望这有助于:

# imported dictionary contains 1800 english:american spelling key:value pairs.
mydict = {
    'color': 'colour',
}


def replace_all(text, mydict):
    for english, american in mydict.iteritems():
        text = text.replace(american, english)
    return text

try:
    with open('new_output.txt', 'w') as new_file:
        with open('test_file.txt', 'r') as f:
            for line in f:
                new_line = replace_all(line, mydict)
                new_file.write(new_line)
except:
    print "Can't open file!"

你也可以看到我之前问的答案,它包含许多最佳实践建议: Loading large file (25k entries) into dict is slow in Python?

下面是一些关于如何编写更多python的其他技巧:) http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html

祝你好运:)

相关问题 更多 >