我有两个CSV文件:
dump1.csv
:
Primary attribute1 attribute2 attribute3
rowA 3992372839 3778338494 9180339101
rowB 8291392010 3739203044 2840493019
dump2.csv
:
Primary attribute1 attribute2 attribute3
rowA 8911849302 9018383910 8103293202
rowB 7310393021 8301940301 7209301030
现在我想创建第三个CSV文件dump1_dump2.csv
:
- With same headers of
dump1.csv
&dump2.csv
- Match Rows and copy these one by one in the third file.
- Rename the row names prepending
dump1_
anddump2_
respectively to distinguish within these.- After two rows picked up from each dump, third CSV file should put a row there which will give us the
change percentage
.
第三个CSVdump1_dump2.csv
应该如下所示:
Primary attribute1 attribute2 attribute3
dump1_rowA 3992372839 3778338494 9180339101
dump2_rowA 8911849302 9018383910 8103293202
change % 123.22 138.68 -11.73
dump1_rowB 8291392010 3739203044 2840493019
dump2_rowB 7310393021 8301940301 7209301030
change % -11.83 122.02 153.80
我写了一个小片段:
import csv
f1 = open('dump2.csv', 'r')
f2 = open('dump2.csv', 'r')
f3 = open('results.csv', 'w')
c1 = csv.reader(f1)
c2 = csv.reader(f2)
c3 = csv.writer(f3)
finallist = list(c2)
for hosts_row in c1:
row = 1
for final_row in finallist:
results_row = hosts_row
if hosts_row[0] == final_row[0]:
# copy line from dump1.csv, paste it to dump1_dump2.csv and modify hosts_row[0] by appending dump1 & dump2 respectively
results_row_1 = #new row containing content of dump1
results_row_2 = #new row containing content of dump2
break
row = row + 2
c3.writerow(results_row_1)
c3.writerow(results_row_2)
f1.close()
f2.close()
f3.close()
我使用的是python3.6。你知道吗
任何帮助都将不胜感激!你知道吗
欢迎使用Stackoverflow。你知道吗
回答你的问题,你可以使用熊猫更容易和更快的评估。你知道吗
输出:
希望这能解决你的问题,如果不请让我知道。 谢谢
相关问题 更多 >
编程相关推荐