【发布时间】:2017-03-25 03:43:50
【问题描述】:
我有两个 .csv 文件:File1=28 列和 1000 行,File2=29 列和 100 行。该行没有索引,因此,我不知道两个文件中哪些行相同。
对于文件 1 中的每一行,当其他 28 列相同时,我想在 File2 中添加一个值为 29 的新列。
File1:
a,b,c,...,EMPTY
x,y,z,...,EMPTY
File2:
a,b,c,...,B1
x,y,z,...,B2
Output:
a,b,c,...,B1
x,y,z,...,B2
到目前为止,我还处于起步阶段;
with open(('File1.csv', 'rb'), delimiter=';') as test1:
reader = csv.reader(test1)
next(reader, None) # ignore header
test1 = set(row[0:28] for row in reader)
with open(('File2.csv', 'rb'), delimiter=';') as test2:
reader = csv.reader(test2)
next(reader, None) # ignore header
test2 = set(row[0:28] for row in reader)
【问题讨论】:
-
欢迎来到 SO 请查看stackoverflow.com/help/formatting 以获取有关格式化您下一个问题或答案的帮助。
-
匹配的行是否总是在文件中的同一行号?
File2中是否存在缺少信息的行? -
不,它们不在同一行,File1 有 40000 行,File2 有 2000 行。