文件格式 – 文件1,2
1 3 116 36 1
我试过了
Compare two files line by line and generate the difference in another file
和
find difference between two text files with one item per line和http://www.daniweb.com/software-development/python/threads/124932/610058#post610058
既不适合我.
我认为这是字符串比较的问题.我想比较第一列和第二列中的数字.如果它们都不同,我想将它写入第三个文件.
任何帮助都感激不尽!
更新
我在@MK发表评论后发布了以下代码.
f = open("results.txt","w")for line in file("100rwsnMore.txt"): rwsncount += 1 line = line.split() src = line[0] dest = line[1] for row in file("100rwsnDeleted.txt"): row = row.split() s = row[0] d = row[1] if(s != src and d != dest): f.write(str(s)) f.write(' ') f.write(str(d)) f.write('\n')f.close()解决方法 如果你在* nix系统上,最好的通用选项就是使用:
sort filea fileb | uniq -u
但是如果你需要使用Python:
您的代码会在外部文件的每次迭代中重新打开内部文件.在循环外面打开它.
使用嵌套循环的效率低于存储找到的值的第一个循环,然后将第二个值与这些值进行比较.
def build_set(filename): # A set stores a collection of unique items. Both adding items and searching for them # are quick,so it's perfect for this application. found = set() with open(filename) as f: for line in f: # [:2] gives us the first two elements of the List. # Tuples,unlike Lists,cannot be changed,which is a requirement for anything # being stored in a set. found.add(tuple(sorted(line.split()[:2]))) return foundset_more = build_set('100rwsnMore.txt')set_del = build_set('100rwsnDeleted.txt')with open('results.txt','w') as out_file: # Using with to open files ensures that they are properly closed,even if the code # raises an exception. for res in (set_more - set_del): # The - computes the elements in set_more not in set_del. out_file.write(" ".join(res) + "\n")总结
以上是内存溢出为你收集整理的逐行比较两个不同的文件,并在第三个文件中写下差异 – Python全部内容,希望文章能够帮你解决逐行比较两个不同的文件,并在第三个文件中写下差异 – Python所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)