| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 471 人关注过本帖
标题:[求助]有关文本文件差集的操作
只看楼主 加入收藏
阿智兄
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2021-3-8
结帖率:100%
收藏
已结贴  问题点数:18 回复次数:21 
[求助]有关文本文件差集的操作
有两个文本文件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编辑过]

搜索更多相关主题的帖子: 操作 TXT 文本文件 for open 
2021-03-19 13:37
zyb159357
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:25
专家分:113
注 册:2021-3-15
收藏
得分:0 
f1=open("A.txt", "r")
f2=open("B.txt", "r")
A=set(f1.read().split("\n"))
B=set(f2.read().split("\n"))
with open("C.txt", "w") as c:
    for i in (A-B):
        c.write(i+"\n")
f1.close()
f2.close()        
2021-03-19 21:10
zyb159357
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:25
专家分:113
注 册:2021-3-15
收藏
得分:0 
#按你的意思写一个,但效率不如上面的.
f1=open("A.txt", "r")
f2=open("B.txt", "r")
A=f1.readlines() #
B=f2.read()
with open("C.txt", "w") as c:
    for i in A:
        if i.strip("\n") not in B: #A的i行不在B.txt里.
           c.write(i)
f1.close()
f2.close()      
2021-03-19 21:56
阿智兄
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2021-3-8
收藏
得分:0 
回复 3楼 zyb159357
谢谢你的回复。忘了把冒号前面的部分改为不相同的了,并且是乱序的。重新改了下,实际是这样的(只要冒号后面那部分相同的就去除):
A.TXT数据如下:
A1A17A38A40A62A66:01,02,03,05,08,17
A1A20A23A42A62A65:01,02,03,05,13,17
A1A23A31A42A43A65:01,02,03,05,13,33
A1A35A37A42A45A57:01,02,03,06,08,22
A1A3A13A31A36A40:01,02,03,05,14,23
A1A3A14A23A30A38:01,02,03,05,13,25
A1A8A33A35A42A62:01,02,03,06,14,27
A2A20A38A40A45A67:01,02,03,05,13,29
A3A19A26A35A36A67:01,02,03,07,10,15
A3A20A32A38A47A57:01,02,03,06,07,24
A3A37A38A49A50A76:01,02,03,06,18,21
A3A6A32A35A47A57:01,02,03,06,19,24
A8A13A14A17A38A40:01,02,03,04,09,23

B.TXT数据如下:
A1A23A31A42A43A65:01,02,03,05,13,33
A1A25A23A42A67A65:01,02,03,05,13,17
A1A3A14A23A30A38:01,02,03,05,13,25
A2A20A38A42A45A67:01,02,03,05,13,29
A3A17A38A40A62A66:01,02,03,05,08,17
A3A20A32A35A47A57:01,02,03,06,07,24
A3A35A39A42A45A67:01,02,03,06,08,22
A7A3A13A31A37A40:01,02,03,05,14,23
A8A13A14A17A38A40:01,02,03,04,09,23

生成的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

下面是我改过的代码,但也没得到预期结果:
程序代码:

 f3 = open("output.txt", "w")            

 with open('txt001.txt') as f1: 
  for line1 in f1:
   line1.strip('\n').split(':')[1])  #取冒号后面部分
  

 with open('txt002.txt') as f2: 
  for line2 in f2:
   line2.strip('\n').split(':')[1])  #取冒号后面部分
  for i in line1:
   if i not in line2:

 print(i,end="\n",file=f3)

 f3.close



[此贴子已经被作者于2021-3-19 23:54编辑过]

2021-03-19 22:43
zyb159357
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:25
专家分:113
注 册:2021-3-15
收藏
得分:0 
f1=open("A.txt", "r")
f2=open("B.txt", "r")
A=f1.readlines() #
B=f2.read()
with open("C.txt", "w") as c:
    for i in A:
        if i.strip("\n").split(":")[1] not in B: #A的i行后半部分不在B.txt里.
           c.write(i)
f1.close()
f2.close()      
2021-03-20 00:07
阿智兄
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2021-3-8
收藏
得分:0 
回复 5楼 zyb159357
谢谢,刚才测试时文件数据出错导致结果不对,现在找到原因了。
这样的情况怎么办?
如果有多个文件A,B,C,D,E,F...都是跟A做差集运算。相当于A-B-C-D-E-F=?

[此贴子已经被作者于2021-3-20 01:00编辑过]

2021-03-20 00:37
zyb159357
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:25
专家分:113
注 册:2021-3-15
收藏
得分:0 
我把你上面的数据代入, 输出的结果和你的预期一样
2021-03-20 00:47
zyb159357
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:25
专家分:113
注 册:2021-3-15
收藏
得分:0 
f1=open("A.txt", "r")
f2=open("B.txt", "r")
A=f1.readlines() #
B=f2.read()
with open("C.txt", "w") as c:
    for i in A:
        if i.strip("\n").split(":")[1].strip() not in B: #A的i行后半部分不在B.txt里.
        #试试再加一个strip()防止多余空格.
           c.write(i)
f1.close()
f2.close()      
2021-03-20 00:56
阿智兄
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2021-3-8
收藏
得分:0 
回复 8楼 zyb159357
确实是空格的问题。
这样的情况怎么办?
如果有多个文件A,B,C,D,E,F...都是跟A做差集运算。相当于A-B-C-D-E-F=?
2021-03-20 01:01
zyb159357
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:25
专家分:113
注 册:2021-3-15
收藏
得分:0 
f1=open("A.txt", "r")
f2=open("B.txt", "r")
f3=open('D.txt', "r")
f4=open('E.txt', "r")
#f5,f6.....
A=f1.readlines() #
B=f2.read()+f3.read()+f4.read()  #加f5.read(),f6.....合并文件内容.
with open("C.txt", "w") as c:
    for i in A:
        if i.strip("\n").split(":")[1] not in B: #A的i行后半部分不在B.txt里.
           c.write(i)
f1.close()
f2.close()      
2021-03-20 01:24
快速回复:[求助]有关文本文件差集的操作
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.098231 second(s), 8 queries.
Copyright©2004-2021, BCCN.NET, All Rights Reserved