Python 如果文件a中包含文件b,则将文件b的记录打印出来输出到c文件里
答案:2 悬赏:70 手机版
解决时间 2021-01-17 20:39
- 提问者网友:欲望失宠
- 2021-01-16 21:57
Python 如果文件a中包含文件b,则将文件b的记录打印出来输出到c文件里
最佳答案
- 五星知识达人网友:痴妹与他
- 2021-01-16 22:42
用list,dic,open就可以了
outKey = {}
for entry in open("b.txt", "r"):
outKey[entry.strip()] = []
outHand = open("c.txt", "w")
for entry in open("a.txt", "r"):
for value in entry.strip().split(","):
(v1,v2) = value.split("/")
if v2 in outKey:
outHand.write(entry)
break;
outKey = {}
for entry in open("b.txt", "r"):
outKey[entry.strip()] = []
outHand = open("c.txt", "w")
for entry in open("a.txt", "r"):
for value in entry.strip().split(","):
(v1,v2) = value.split("/")
if v2 in outKey:
outHand.write(entry)
break;
全部回答
- 1楼网友:洒脱疯子
- 2021-01-16 23:11
脚本文件:
#!/usr/bin/env python
with open('a.txt') as f:
filea=f.readlines()
with open('b.txt') as f:
fileb=f.readlines()
print ''.join([ w for c in fileb for w in filea if c.strip() in w ])
文件a:
root@ubuntu:~/python/1211# more a.txt
10/05766798607,11/20050325191329,29/0.1,14/05766798607
10/05767158557,11/20050325191329,29/0.08,14/05767158557
文件b:
root@ubuntu:~/python/1211# more b.txt
05766798607
05766798608
05766798609
打印结果:
10/05766798607,11/20050325191329,29/0.1,14/05766798607
#!/usr/bin/env python
with open('a.txt') as f:
filea=f.readlines()
with open('b.txt') as f:
fileb=f.readlines()
print ''.join([ w for c in fileb for w in filea if c.strip() in w ])
文件a:
root@ubuntu:~/python/1211# more a.txt
10/05766798607,11/20050325191329,29/0.1,14/05766798607
10/05767158557,11/20050325191329,29/0.08,14/05767158557
文件b:
root@ubuntu:~/python/1211# more b.txt
05766798607
05766798608
05766798609
打印结果:
10/05766798607,11/20050325191329,29/0.1,14/05766798607
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯