【问题标题】:Comparing 2 CSV files using python with different rows使用具有不同行的python比较2个CSV文件
【发布时间】:2018-06-26 04:45:24
【问题描述】:

我有两个 csv 文件,分别是 one.csv 和 two.csv。文件格式为 One.csv:

ID1 , att 1 , att 2

两个.csv

ID2, att2, att1 

现在我想在 att1 和 att2 相同的地方打印 ID1、ID 2、att1、att2。我已经写了这段代码,但它只打印了一个这样的案例但是还有更多这样的案例

import csv
path1 ="/home/vishver/Desktop/"
file1 =open(path1 +"one.csv","r")
file2 =open(path1 +"two.csv","r")

reader1 =csv.reader(file1)
reader2 =csv.reader(file2)
count =0
for line1 in reader1:
	for line2 in reader2:	
		if line1[1] == line2[2] or line1[2] == line2[1]:
			t=line2[0],line1[0],line1[1],line1[2]			
			print(t)
			count+=1
	
print count

【问题讨论】:

  • 你会使用 Pandas 吗?对我来说似乎是最简单的方法。
  • reader2 =csv.reader(file2)移动到外部for循环内。
  • @Ilayaraja 试过了!它没有帮助
  • @ncfirth 任何关于如何使用 pandas 的指示
  • @user3767047 这可能是关于创建你的 csvs 的两个 pandas 数据帧,然后在 id 上创建joining 它们,然后对生成的连接数据帧进行一些简单的操作。

标签: python python-2.7 file csv


【解决方案1】:

如果您只想打印公共属性的总数,请尝试以下操作:

path1 ="/home/vishver/Desktop/"
file1 =open(path1 +"one.csv","r")
file2 =open(path1 +"two.csv","r")

reader1 =csv.reader(file1)
reader2 =csv.reader(file2)

count =0
file1_att1 = []
file1_att2 = []
file2_att1 = []
file2_att2 = []


for line1 in reader1:
    file1_att1.append(line[1])
    file1_att2.append(line[2])

for line1 in reader1:
    file2_att1.append(line[1])
    file2_att2.append(line[2])

common1 = (set(file1_att1).intersection(file2_att2))
common2 = (set(file1_att2).intersection(file2_att1))

print common1+common2

【讨论】:

  • 如何得到对应的id1和id2?
  • @user3767047 对 common1 和 common2 属性列表运行 forloop 并分别从 file1 和 file2 中找出对应的 id1 和 id2。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 2014-05-27
  • 1970-01-01
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多