【发布时间】:2012-11-09 18:21:33
【问题描述】:
我需要用不同的单词替换文本文档中长度为 4 的所有单词。
例如,如果文本文档包含短语“我喜欢吃非常热的汤”,那么“喜欢”、“非常”和“汤”等词将被替换为“某物”
然后,它需要使用更改后的短语创建一个新文档,而不是覆盖原始文本文档。
这是我目前所拥有的:
def replacement():
o = open("file.txt","a") #file.txt will be the file containing the changed phrase
for line in open("y.txt"): #y.txt is the original file
line = line.replace("????","something") #see below
o.write(line + "\n")
o.close()
我试过改变“??????”像
(str(len(line) == 4)
但这没有用
【问题讨论】:
标签: python string python-3.2