Python CSV比较

2024-04-20 05:20:26 发布

您现在位置:Python中文网/ 问答频道 /正文

这个脚本比较两个csv文件…两列plz帮助我修改这个脚本如果sample1.csv和sample2.csv有超过2列或1列。你知道吗

f1_in = open("sample1.csv","r")
next(f1_in,None)
f1_dict = {}
for line in f1_in:
  l = line.split(',')
  f1_dict[l[0]. strip()] = l[1]. strip() 
  l.sort()
f1_in.close()

f2_in = open("sample2.csv","r")
next(f2_in,None)
f2_dict = {}
for line in f2_in:
  l = line.split(',')
  f2_dict[l[0]. strip()] = l[1]. strip()
  l.sort()
f2_in.close()


f_same = open("same.txt","w")
f_different = open("different.txt","w")

for k1 in f1_dict.keys():
  if k1 in f2_dict.keys() \
      and f2_dict[k1] == f1_dict[k1]:
    f_same.write("{0}, {1}\n". format(str(k1)+" "+str(f1_dict[k1]),
                                    str(k1)+" "+str(f2_dict[k1])))

  elif not k1 in f2_dict.keys():
    f_different.write("{0}, {1}\n". format(str(k1)+" "+str(f1_dict[k1]),
                                           "------"))
  elif not f2_dict[k1] == f1_dict[k1]:
    f_different.write("{0}, {1}\n". format(str(k1)+" "+str(f1_dict[k1]),
                                           str(k1)+" "+str(f2_dict[k1])))

f_same.close()
f_different.close()

例如:如果我的源文件有Name和Salary作为标题,值为A 20000 B 15000 C 10000 D 10000,目标文件也有Name和Salary作为标题,值为A 40000 D 10000 B 15000 C 10000 E 8000…我的输出应该不同线路:A 20000A 40000 D 10000-----(目标中无文件)--(源代码中无文件)E 8000,公共行为B15000 B 15000 C 10000 C 10000


Tags: 文件csvinforcloselinek1open
1条回答
网友
1楼 · 发布于 2024-04-20 05:20:26

如果将列视为字典中的键/值对,则不能将代码扩展到两个以上的列也就不足为奇了。你知道吗

你必须把它们看作“集合中的元素”。我理解这就是为什么您不使用csv模块或difflib模块的原因:因为您不关心行在两个文件中是否(几乎)以相同的顺序出现,而是关心它们是否完全出现。你知道吗

举个例子:

import itertools


def compare(first_filename, second_filename):
    lines1 = set()
    lines2 = set()
    with open(first_filename, 'r') as file1, \
            open(second_filename, 'r') as file2:
        for line1, line2 in itertools.izip_longest(file1, file2):
            if line1:
                lines1.add(line1)
            if line2:
                lines2.add(line2)
    print "Different lines"
    for line in lines1 ^ lines2:
        print line,
    print " -"
    print "Common lines"
    for line in lines1 & lines2:
        print line,

请注意,这段代码将在两个文件上找到差异,而不仅仅是存在于f1上的内容,而不是存在于f2上的内容,正如您的示例所做的那样。然而,它无法判断差异来自何处(因为这似乎不是问题的要求)。你知道吗

检查它是否工作

In [40]: !cat sample1.csv
bacon, eggs, mortar
whatever, however, whenever
spam, spam, spam

In [41]: !cat sample2.csv
guido, van, rossum
spam, spam, spam

In [42]: compare("sample1.csv", "sample2.csv")
Different lines
whatever, however, whenever
guido, van, rossum
bacon, eggs, mortar
 -
Common lines
spam, spam, spam

相关问题 更多 >