我有两个大约1GB的文本文件,其中每行有60列。 每个文件中有6列是要比较的键。在
示例:
file1:
4|null|null|null|null|null|3590740374739|20077|7739662|75414741|
file2:
4|null|11|333|asdsd|null|3590740374739|20077|7739662|75414741|
这里两行相等,因为列7、8、9和10在两个文件(键)中是相同的。 我尝试了一个不考虑键的文件比较示例,这很好,但是我需要根据键进行比较,而不是每行中的字符对字符进行比较。在
下面是我在不考虑键的情况下进行比较的代码示例。在
matched = open('matchedrecords.txt','w')
with open('srcone.txt') as b:
blines = set(b)
with open('srctwo.txt') as a:
alines = set(a)
with open('notInfirstSource.txt', 'w') as result:
for line in alines:
if line not in blines:
result.write(line)
else:
matched.write(line)
with open('notInsecondSource.txt', 'w') as non:
for lin in blines:
if lin not in alines:
non.write(lin)
matched.close()
这是一种基于键/列比较行的方法,但我不确定它有多有效。在
最后,我可以使用字典在很短的时间内实现这一点。 i、 e一个370 MB的数据与270MB的数据文件相比,最多50秒(使用元组作为键)。 脚本如下:
有什么可以改进的吗?建议我! 谢谢
根据ActiveState上的recipe for KeyedSets的提示,您可以构建一个集合,然后简单地使用set intersection和set difference来生成结果:
相关问题 更多 >
编程相关推荐