How to delete specific strings from a file?

后端 未结 5 2056
花落未央
花落未央 2020-12-02 01:22

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:

5条回答
  •  臣服心动
    2020-12-02 01:33

    To remove the string within the same file, I used this code

    f = open('./test.txt','r')
    a = ['word1','word2','word3']
    lst = []
    for line in f:
        for word in a:
            if word in line:
                line = line.replace(word,'')
        lst.append(line)
    f.close()
    f = open('./test.txt','w')
    for line in lst:
        f.write(line)
    f.close()
    

提交回复
热议问题