现有两个txt文本文档,A文档:B文档:
想要通过python脚本来生成
的文档,实际操作时的数据量比举的例子大得多,所以需要脚本来实现。多谢诸位大大的帮助!
fp = r'c.txt'
for line_b in open("B.txt", encoding='gb18030', errors = 'ignore'):
num_b = line_b[:line_b.index(";")]
for line_a in open("A.txt", encoding='gb18030', errors = 'ignore'):
if int(num_b) == int(line_a):
with open(fp, 'a') as f:
f.write(line_b)
生成结果在c.txt中
先把A生成一个字典。然后遍历B文档,判断如果key 在A里,那么就把这条数据写下来。
可以用内置函数filter
Init signature: filter(self, /, *args, **kwargs)
Docstring:
filter(function or None, iterable) --> filter object
Return an iterator yielding those items of iterable for which function(item)
is true. If function is None, return the items that are true.
和我提的问题有点相识,给你参考下
https://ask.csdn.net/questions/761503
这类数据比对与处理用python,数据量大就会产生效率低下,