【问题标题】:Remove some words replace some other words from a txt file从 txt 文件中删除一些单词替换一些其他单词
【发布时间】:2013-08-20 02:58:16
【问题描述】:

我有一个包含多行文本的 txt 文件 (myText.txt)。

我想知道:

  • 如何创建需要删除的单词列表(我想自己设置单词)
  • 如何创建需要替换的单词列表

例如,如果 myText.txt 是:

    The ancient Romans influenced countries and civilizations in the following centuries.  
Their language, Latin, became the basis for many other European languages. They stayed in Roma for 3 month. 
  • 我想删除我想替换的“the”“和”“in” “古老”的“古代”
  • 我想替换“月”和“世纪” 按“年”

【问题讨论】:

    标签: python string file-io replace


    【解决方案1】:

    你总是可以使用正则表达式:

    import re
    
    st='''\
    The ancient Romans influenced countries and civilizations in the following centuries.  
    Their language, Latin, became the basis for many other European languages. They stayed in Roma for 3 month.'''
    
    deletions=('and','in','the')
    repl={"ancient": "old", "month":"years", "centuries":"years"}
    
    tgt='|'.join(r'\b{}\b'.format(e) for e in deletions)
    st=re.sub(tgt,'',st)
    for word in repl:
        tgt=r'\b{}\b'.format(word)
        st=re.sub(tgt,repl[word],st)
    
    
    print st
    

    【讨论】:

    • 你好工作非常好。只是一个问题,有时我的文字中有“+”和“-”号。但是似乎Python不接受删除=('and','in','the','+','-') 有没有特殊的方法可以输入这些字符?谢谢你
    • 有些字符对正则表达式有意义,例如+- 我的建议是花一些时间在正则表达式教程网站上学习这些字符。 Regex101 不错。
    【解决方案2】:

    这应该可以解决问题。您使用列表来存储要删除的对象,然后遍历列表并从内容字符串中删除列表中的每个元素。然后,您使用字典来存储您现在拥有的单词以及要替换它们的单词。您还可以遍历这些并用替换词替换当前词。

    def replace():
        contents = ""
        deleteWords = ["the ", "and ", "in "]
        replaceWords = {"ancient": "old", "month":"years", "centuries":"years"}
    
        with open("meText.txt") as f:
        contents = f.read()
        for word in deleteWords:
        contents = contents.replace(word,"")
    
        for key, value in replaceWords.iteritems():
        contents = contents.replace(key, value)
        return contents
    

    【讨论】:

    • 感谢您的帮助。我只是收到一条错误消息“AttributeError:'dict'对象没有属性'iteritems'”我只是最新版本的Python。正常吗?谢谢。
    • 如果你使用的是 python 3,那么说 replaceWords.items()
    【解决方案3】:

    使用列表进行删除,使用字典进行替换。它应该看起来像这样:

     def processTextFile(filename_in, filename_out, delWords, repWords):
    
    
        with open(filename_in, "r") as sourcefile:
            for line in sourcefile:
                for item in delWords:
                    line = line.replace(item, "")
                for key,value in repWords.items():
                    line = line.replace(key,value)
    
                with open(filename_out, "a") as outfile:
                    outfile.write(line)
    
    
    
    if __name__ == "__main__":
        delWords = []
        repWords = {}
    
        delWords.extend(["the ", "and ", "in "])
        repWords["ancient"] = "old"
        repWords["month"] = "years"
        repWords["centuries"] = "years"
    
        processTextFile("myText.txt", "myOutText.txt", delWords, repWords)
    

    请注意,这是为 Python 3.3.2 编写的,这就是我使用 items() 的原因。如果使用 Python 2.x,请使用 iteritems(),因为我认为它更有效,尤其是对于大型文本文件。

    【讨论】:

    • 感谢您提供此代码。哇有很多方法可以实现我的目标:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    相关资源
    最近更新 更多