[求助]有关文本文件差集的操作
有两个文本文件A.TXT,B.TXT,要将A文件中冒号后面部分含有B的那部分数据去掉后生成新文件C.TXT,例如:A.TXT数据如下:
A8A13A14A17A38A40:01,02,03,04,09,23
A1A17A38A40A62A66:01,02,03,05,08,17
A1A20A23A42A62A65:01,02,03,05,13,17
A1A3A14A23A30A38:01,02,03,05,13,25
A2A20A38A40A45A67:01,02,03,05,13,29
A1A23A31A42A43A65:01,02,03,05,13,33
A1A3A13A31A36A40:01,02,03,05,14,23
A3A20A32A38A47A57:01,02,03,06,07,24
A1A35A37A42A45A57:01,02,03,06,08,22
A1A8A33A35A42A62:01,02,03,06,14,27
A3A37A38A49A50A76:01,02,03,06,18,21
A3A6A32A35A47A57:01,02,03,06,19,24
A3A19A26A35A36A67:01,02,03,07,10,15
B.TXT数据如下:
A8A13A14A17A38A40:01,02,03,04,09,23
A1A17A38A40A62A66:01,02,03,05,08,17
A1A20A23A42A62A65:01,02,03,05,13,17
A1A3A14A23A30A38:01,02,03,05,13,25
A2A20A38A40A45A67:01,02,03,05,13,29
A1A23A31A42A43A65:01,02,03,05,13,33
A1A3A13A31A36A40:01,02,03,05,14,23
A3A20A32A38A47A57:01,02,03,06,07,24
A1A35A37A42A45A57:01,02,03,06,08,22
生成的C.TXT如下:
A1A8A33A35A42A62:01,02,03,06,14,27
A3A37A38A49A50A76:01,02,03,06,18,21
A3A6A32A35A47A57:01,02,03,06,19,24
A3A19A26A35A36A67:01,02,03,07,10,15
帮忙看看怎么修改?
程序代码:
def test(): new_text_path="output.txt" oldText = open("txt002.txt","r") newText = open("txt001.txt","r") new_file_path=open("output.txt","w") for oldline in oldText: newText = open("txt001.txt", "r") for newline in newText: new_text = [ i for i in oldText if i not in newText ] new_file_path.write(new_text) #从新写入到新的文件 if __name__ == '__main__': test()
[此贴子已经被作者于2021-3-19 15:22编辑过]