【发布时间】: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