比较两个CSV文件,搜索匹配的标头并计算差异-Python

时间:2019-06-04 13:38:45

标签: python csv compare

我有两个csv文件。 我的参考文件如下:

file1.csv:
h1;h2;latitude;longitude
X;Y;49.000;15.000
X;Y;49.001;15.001

第二个文件有许多不必要的值,例如:

file2.csv:
h1;h2;latitude;longitude;h3;h4;h5;h6;..
X,Y;49.002;15.002
X,Y;49.003;15.003
false;false;49.003;15.003
X;false;49.003;15.003
...
..(about 120 entries)

首先,我需要检查标题名称是否匹配(名称相等),然后我需要检查h1:X(来自file1)和h2:X(来自file2)是否相等,如果是,< / p>

检查h2:Y(来自文件1)和h2:Y(来自文件2)是否相等

计算file1(lat + lon)和file2(lat + lon)之间的距离,并将输出写入results.csv中,例如:

results.csv:


    h1;h2;latitudeFile1;longitudeFile2;latitudeFile2;longitudeF2;calculatedDistance
X;Y;49.000;15.000;49.002;15.002,xxx km



(The distance calculation is done via the haversine formula)

到目前为止,我的初稿看起来像这样:

f1 = open('file1.csv', 'rb')
f2 = open('file2.csv', 'rb')
f3 = open('results.csv', 'wb')

c1 = csv.reader(f1, delimiter=',')
c2 = csv.reader(f2, delimiter=',')
c3 = csv.writer(f3)   

reader1 = csv.reader(f1, delimiter=',', quotechar='"')
rowF1 = next(reader1)
reader2 = csv.reader(f2, delimiter=',', quotechar='"')
rowF2 = next(reader2)

#writing header to results.csv
c3.writerow(["signType", "signValue", "latitudeFile1", "longitudeFile1", "latitudeFile2", "longitudeFile2",
             "calculatedDistance"])

if (rowF1[0] == rowF2[0]) and (rowF1[1] == rowF2[1]):
    next(f3)
    c3.writerow[0] = rowF1[0]
    c3.writerow[1] = rowF1[1]
    c3.writerow[2] = rowF1[2]
    c3.writerow[3] = rowF1[4]
    c3.writerow[5] = rowF1[5]
    #c3.writerow[6] = haversine(rowF1[2], rowF1[3], rowF2[2], rowF2[3]))

else:
    print('no match')

任何帮助将不胜感激:-)

0 个答案:

没有答案