如何从文件中删除特定字符串? [英] How to delete specific strings from a file?
本文介绍了如何从文件中删除特定字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据文件(非结构化、凌乱的文件),我必须从中清理特定的字符串列表(删除字符串).
I have a data file (unstructured, messy file) from which I have to scrub specific list of strings (delete strings).
这是我正在做但没有结果的事情:
Here is what I am doing but with no result:
infile = r"messy_data_file.txt"
outfile = r"cleaned_file.txt"
delete_list = ["firstname1 lastname1","firstname2 lastname2"....,"firstnamen lastnamen"]
fin=open(infile,"")
fout = open(outfile,"w+")
for line in fin:
for word in delete_list:
line = line.replace(word, "")
fout.write(line)
fin.close()
fout.close()
当我执行文件时,出现以下错误:
When I execute the file, I get the following error:
NameError: name 'word' is not defined
推荐答案
readlines
方法返回 行 的列表,而不是单词,因此您的代码仅在您的一个单词单独位于一行时才有效.
The readlines
method returns a list of lines, not words, so your code would only work where one of your words is on a line by itself.
由于文件是迭代器,因此可以更容易完成:
Since files are iterators over lines this can be done much easier:
infile = "messy_data_file.txt"
outfile = "cleaned_file.txt"
delete_list = ["word_1", "word_2", "word_n"]
with open(infile) as fin, open(outfile, "w+") as fout:
for line in fin:
for word in delete_list:
line = line.replace(word, "")
fout.write(line)
这篇关于如何从文件中删除特定字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文