搜索单词,并使用 fileinput 在 Python 中的文件中替换包含该单词的整行 [英] Search the word, and replace the whole line containing the word in a file in Python using fileinput

查看:21
本文介绍了搜索单词,并使用 fileinput 在 Python 中的文件中替换包含该单词的整行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本文件中搜索特定单词.对于存在单词的每一行,我想用一个新文本完全改变该行.

I want to search a particular word in a text file. For each line,where the word is present, I want to completely change the line by a new text.

我想使用 python 的 fileinput 模块来实现这一点.有两个观察结果,我看到以下变化:-

I want to achieve this using fileinput module of python. There are two observation, I am seeing with following variations :-

代码片段 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,即文件内容只有

The above piece of code wipes out all the content of the file, and writes the new_text i.e. the file content is only

mov9 = 爱丽丝梦游仙境

mov9 = Alice in Wonderland

代码片段 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()

上面这段代码,即使在找到text的地方添加了需要的行,即new_text,但并没有删除该行,而是保留了之前的数据.

The above piece of code, even though adds the needed line i.e. new_text where text is found, but doesn't deletes the line, but keeps the previous data also.

也就是说,如果该行更早:-

That is if the line was earlier :-

mov9 = Fast & Furios

运行上面的代码后,它变成了:-

after running the above piece of code it becomes :-

mov9 = Alice in WonderlandFast & Furios

文件的其他内容保持不变,不会像代码片段 1 中的代码那样被删除.

And other content of the files remain untouched, not deleted as in code in Code piece 1.

但我的目标是找到单词 mov9 =,无论它出现什么,我都想将整行替换为 mov9 = Alice in Wonderland.

But my goal is to find the word mov9 =, and whatever is present along with it, I want to replace the whole line as mov9 = Alice in Wonderland.

我怎样才能做到这一点?提前致谢....

How can I achieve that? Thanks in advance....

推荐答案

我意识到我错了,只是缩进了.在问题中提到的代码段 1 中,如果我从 if 的范围内带来打印线",即如果我将其缩小,那么就解决了...

I realized that I was wrong by just an indentation. In the code piece 1 mentioned in the question, if I am bringing the 'print line,' from the scope of if i.e. if i outdent it, then this is solved...

由于这一行在 if 的范围内,因此只有这行 new_text 被写入文件,其他行没有被写入,因此文件只剩下 new_text.所以,代码片段应该如下:-

As this line was inside the scope of if, hence, only this new_text was being written to the file, and other lines were not being written, and hence the file was left with only the new_text. So, the code piece should be as follow :-

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()

另外,萨克森的罗尔夫 &Padraic Cunningham 有点相似.

Also, the second solution given by Rolf of Saxony & the first solution by Padraic Cunningham is somehow similar.

这篇关于搜索单词,并使用 fileinput 在 Python 中的文件中替换包含该单词的整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆