我想搜索文本文件中的特定单词。对于存在单词的每一行,我想通过新文本完全更改该行。
我想使用python的fileinput
模块来实现这一点。有两个观察,我看到以下变化: -
代码1: -
text = "mov9 = " # if any line contains this text, I want to modify the whole line.
new_text = "mov9 = Alice in Wonderland"
x = fileinput.input(files="C:\Users\Admin\Desktop\DeletedMovies.txt", inplace=1)
for line in x:
if text in line:
line = new_text
print line,
x.close()
上面的代码消除了文件的所有内容,并写入new_text
,即文件内容仅
mov9 =爱丽丝梦游仙境
代码2: -
text = "mov9 = " # if any line contains this text, I want to modify the whole line.
new_text = "mov9 = Alice in Wonderland"
x = fileinput.input(files="C:\Users\Admin\Desktop\DeletedMovies.txt", inplace=1)
for line in x:
if text in line:
line = line.replace(text, new_text)
print line,
x.close()
上面的代码,即使添加了所需的行,即new_text
,其中text
被找到,但不删除该行,但也保留以前的数据。
如果该行更早,那就是: -
mov9 = Fast & Furios
运行上面的代码后,它变为: -
mov9 = Alice in WonderlandFast & Furios
文件的其他内容保持不变,不会像代码段1中的代码那样被删除。
但我的目标是找到mov9 =
这个词,以及随之而来的任何内容,我想将整行替换为mov9 = Alice in Wonderland
。
我怎样才能实现这一目标?提前谢谢....
答案 0 :(得分:3)
我意识到只是一个缩进我错了。在问题中提到的代码片1中,如果我带来了'打印行,'从if的范围来看,如果我突然,那么这就解决了......
因为这一行在if
的范围内,所以只有这个new_text
被写入文件,而其他行没有被写入,因此文件只留下了new_text
。因此,代码片应如下: -
text = "mov9 = " # if any line contains this text, I want to modify the whole line.
new_text = "mov9 = Alice in Wonderland"
x = fileinput.input(files="C:\Users\Admin\Desktop\DeletedMovies.txt", inplace=1)
for line in x:
if text in line:
line = new_text
print line,
x.close()
此外,Rolf of Saxony&给出了第二个解决方案。 Padraic Cunningham的第一个解决方案在某种程度上是相似的。
答案 1 :(得分:0)
建议 vyscond :
import re
yourTxt = ''.join(open('file.txt').readlines())
yourTxt = re.sub(r'\bmov9\b', r'mov9 = Alice in Wonderland', yourTxt)
#Save the new text on disk
f = open('newTxt.txt', 'w')
f.write(yourTxt)
f.close()
答案 2 :(得分:0)
您清空文件是因为您只在找到匹配项时写入,您需要始终写下这些行:
import sys
text = "mov9 = " # if any line contains this text, I want to modify the whole line.
new_text = "mov9 = Alice in Wonderland\n"
x = fileinput.input(files="C:\Users\Admin\Desktop\DeletedMovies.txt", inplace=1)
for line in x:
if text in line:
line = new_text
sys.stdout.write(line)
如果找到匹配项,该行将设置为new_text
,因此sys.stdout.write(line)
将写入原始行或new_text
。另外,如果您确实想要查找以text
开头的行,请使用if line.startswith(text):
您也可以写信给tempfile
并替换原来的内容:
from shutil import move
from tempfile import NamedTemporaryFile
text = "mov9 = " # if any line contains this text, I want to modify the whole line.
new_text = "mov9 = Alice in Wonderland\n"
with open("C:\Users\Admin\Desktop\DeletedMovies.txt") as f, NamedTemporaryFile("w", dir=".", delete=False) as tmp:
for line in f:
if text in line:
line = new_text
tmp.write(line)
move(tmp.name, "C:\Users\Admin\Desktop\DeletedMovies.txt")
答案 3 :(得分:0)
我建议同时使用两个文件替换文本并将替换的数据写入另一个文件,然后只删除旧文件。
path="Hello.txt"
path2="Hello2.txt"
file1_open=open(path,"r")
file2_open=open(path2,"w")
for line in file1_open:
if "a" in line:
print "found"
line=line.replace('a',"replaced")
print line
file2_open.write(line)
现在您可以在内部删除file1_open文件。 虽然它很小而且简单,但是当使用大文件时可能会在CPU上加载。
答案 4 :(得分:0)
看起来这可能不是这个主题的唯一解决方案,现在我已经重新阅读了答案,但是当我在这里敲响了代码时。 保持简单:
import os
text = "mov9 = " # if any line contains this text, I want to modify the whole line.
new_text = "mov9 = Alice in Wonderland "
new=open("new.txt",'w')
with open('DeletedMovies.txt', 'r') as f:
for line in f.readlines():
if text in line:
line = line.replace(text, new_text)
print line
new.write(line)
new.close()
os.rename('DeletedMovies.txt', 'DeletedMovies.txt.old')
os.rename('new.txt', 'DeletedMovies.txt')
这取决于你期望这个文件有多大。
编辑:
如果您决定使用fileinput
,尽管对于不熟悉该软件包的人来说这会让您感到困惑,因为它一点都不清楚发生了什么,那么您几乎就在那里使用现有的代码。 />
以下应该有效:
import fileinput
text = "mov9 = " # if any line contains this text, I want to modify the whole line.
new_text = "mov9 = Alice in Wonderland "
for line in fileinput.input("DeletedMovies.txt", inplace = 1):
if text in line:
print line.replace(line,new_text)
else:
print line.strip()
答案 5 :(得分:0)
对 Rolf of Saxony 的答案略有改进,这样您就可以运行脚本而无需对值进行硬编码。
import fileinput
import sys
filename=sys.argv[1]
old_value=sys.argv[2]
new_value=sys.argv[3]
with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
for line in file:
if old_value in line:
print(line.replace(line,new_value))
else:
print(line.strip())
现在从命令行以 replace.py filename.txt old_value New_Value
运行它