如何比较CSV文件中的两个表?

时间:2019-09-27 08:56:20

标签: python csv

我需要从一个csv文件中提取一列,并将此列与另一个csv文件中的另一列进行比较以找到匹配项。

我无法使用Panda,我提取了表格并将其卡住。

def first():
    with open('1.csv') as csv_file:
        for line in csv_file.readlines():
            array = line.split(',')
            list_pk = array[1]

def sec():
    with open('2.csv') as csv_file:
        for line in csv_file.readlines():
            array = line.split(',')
            list_fk = array[0]

1 个答案:

答案 0 :(得分:0)

我希望它将对您有帮助

def findMatch():
  with open('old.csv', 'r',  newline='') as t1, open('new.csv', 'r',  newline='') as t2:
    for line1,line2 in zip(t1,t2):
      if line1.split(' ')[colum_index]!=line2.split(' ')[colum_index]:
        print(line1,line2)
findMatch()
  • 有关更多帮助,请参阅csv doc
  • 有关zip / izip,请参考zip/izip

使用zip_longest

from itertools import zip_longest
def findMatch():
  with open('old.csv', 'r',  newline='') as t1, open('new.csv', 'r',  newline='') as t2:
    for line in zip_longest(t1,t2):
      print(line)
      if line[0]!=line[1]:
        print("nq")
findMatch()

zip_最长引用:-zip_longest