【问题标题】:Replace string python txt text替换字符串python txt文本
【发布时间】:2018-08-29 15:57:23
【问题描述】:

我有一本 txt 格式的书。我想创建2个新文本:首先,我想用Paul_1替换字符串"Paul"的所有出现,第二个用Paul_2替换。 我写了这段代码:

with open("book.txt", 'r') as original, \
        open("book_1.txt", 'w') as mod1, \
        open("book_2.txt", 'w') as mod2:
    for line in original:
        words = line.split()
        for word in words:
            s="Paul"
            if(word == s):
                mod1.write(word + "_1 ")
                mod2.write(word + "_2 ")
            else:
                mod1.write(word + " ")
                mod2.write(word + " ")
        mod1.write("\n")
        mod2.write("\n")

有一个问题,经常会跳过一些Paul,因此,最后,我在同一个文档中同时拥有PaulPaul_1(以及PaulPaul_2)。问题出在哪里?

【问题讨论】:

  • 跳过的有可能是Paul,Paul. 之类的吗?
  • @bgse 是的,我现在注意到它跳过了像 Paul 和 Paul' 这样的字符串。我该如何解决?
  • 你可以使用startswith()的方法或者用replace删除标点符号(使用正则表达式)或者比较word[:-1]比较没有最后一个字母/符号的单词
  • @Camilla8 str.split() 默认情况下使用空格作为分隔符拆分您的字符串,它并不真正适合您的需求,因为如果您自己指定一个分隔符,您只能由一个分隔符拆分。你可能想看看re.split()

标签: python string file replace split


【解决方案1】:

这应该会有所帮助。

import re

with open("book.txt", 'r') as original, \
        open("book_1.txt", 'w') as mod1, \
        open("book_2.txt", 'w') as mod2:
    data = original.read()
    data_1 = re.sub(r"\bPaul\b", 'Paul_1', data)   #Replace any occurrence of Paul with Paul_1 
    data_2 = re.sub(r"\bPaul\b", 'Paul_2', data)   #Replace any occurrence of Paul with Paul_2 
    mod1.write(data_1 + r"\n")
    mod2.write(data_2 +  r"\n")

【讨论】:

  • 最后 2 条指令中的 'r' 有什么作用?
  • 应该考虑到像"Paula is a nice lady.".replace("Paul", "Paul_1") 这样的边缘情况,因为问题是关于书本的,这并不太牵强。
  • @Rakesh 如果 Paul 是另一个的子字符串,您的代码会出现问题。例如,如果有 PostPaul,我会得到 PostPaul_1,而我的目标是只替换 Paul 而不是 PostPaul 之类的字符串
  • 哦,好的。在这种情况下,您可能需要正则表达式。让我明天早上试着做一个。
  • 更新了 sn-p。
猜你喜欢
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-06
  • 2015-11-04
  • 1970-01-01
相关资源
最近更新 更多