源码

Python:从文件中删除特定字符串


我有一个数据文件(未构造的凌乱文件),我必须从中擦除特定的字符串列表(删除字符串).

这是我正在做的但没有结果:

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

当我执行该文件时,我收到以下错误:

NameError: name 'word' is not defined

请帮忙!

(0)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/pythoncongwenjianzhongshanchutedingzifuchuan.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9月 26, 2019 at 12:09 上午

热评文章