我有两个.csv
个文件,只有一行,但有很多列。我希望比较列中的数据(前3列除外)并输出包含文件减法的新.csv
,计算为baseline - test
。
test1.csv
20170223, 433000000, 8k, -50, -50, -10, -50, -50
baseline.csv
20170223, 433000000, 8k, -50, -50, -50, -50, -50
生成的.csv
文件应为:
20170223, 433000000, 8k, 0, 0, -40, -0, -0
我能够调出.csv
个文件,但是列位置和计算很难实现。
这是我到目前为止所做的:
import csv
with open('test001.csv', 'r') as f:
reader = csv.reader(f, delimiter = ',')
first_list = list(reader)
f.close()
with open('test002.csv', 'r') as f:
reader = csv.reader(f)
second_list = list(reader)
f.close()
result_list = list()
list_a = list()
list_b = list()
for row in first_list:
for x in range(0, 6):
result_list.append(row[x])
for x in range(6, len(row)-1):
list_a.append(row[x])
for row in second_list:
for x in range(6, len(row)-1):
print(row[x])
list_b.append(row[x])
for x in range(0, len(list_a)-1):
a = float(list_a[x])
b = float(list_b[x])
c = a-b
result_list.append(c)
myfile = open('difference.csv', 'w')
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerow(result_list)
myfile.close()
答案 0 :(得分:0)
假设您已将这些文件读入两个列表one
和two
然后您可以使用zip
按元素逐个元素地比较这些列表,如下所示:
>>> one = [1, 2, 3]
>>> two = [4, 5, 6]
>>> for o, t in zip(one, two):
... print(o, t)
...
(1, 4)
(2, 5)
(3, 6)
>>>
而不是print
实现你自己的逻辑。要从第4列开始,只需使用
`zip(one, two)[3:]`
答案 1 :(得分:0)
您可以像这样使用pandas
:
import pandas as pd
df1 = pd.read_csv('test1.csv', header=None)
df2 = pd.read_csv('baseline.csv', header=None)
diff = df1.copy()
diff[diff.columns[3:]] -= df2[df2.columns[3:]]
diff.to_csv('difference.csv', index=False, header=None)