简体   繁体   English

我可以合并 CSV 文件并将第一列添加在一起吗?

[英]Can I merge CSV files and add the first columns together?

I have multiple CSV with counts of values, but not all CSV values have the same order of the objects they are counting, and some have them missing all together.我有多个 CSV 值计数,但并非所有 CSV 值都具有相同的计数对象顺序,并且有些值一起丢失。 Similar to this:与此类似:

5,value1
6,value3
12,value4
6,value1
3,value2
8,value4
10,value5
2,value1
3,value5

I want to merge these CSV files.我想合并这些 CSV 文件。 Expected output of the 3 above would be:上述 3 的预期 output 将是:

13,value1
3,value2
6,value3
20,value4
13,value5

I've tried to cat both files and sort on the second column, and that gets me the information, just the second columns are not merged together and first columns added together.我试图对两个文件进行分类并对第二列进行排序,这让我得到了信息,只是第二列没有合并在一起,第一列加在一起。 The join command gives me errors about it not being sorted, and I've also tried join -e on both files but also get an error join: conflicting empty-field replacement strings . join 命令给了我关于它没有被排序的错误,我也尝试了 join -e 在这两个文件上,但也得到了一个错误join: conflicting empty-field replacement strings I've been using bash up to this point but also have Python installed.到目前为止,我一直在使用 bash,但也安装了 Python。

  • use collections.defaultdict(int)使用collections.defaultdict(int)
  • use the csv module to read and iterate over the files使用 csv 模块读取和迭代文件
  • for each line of each file对于每个文件的每一行
    • use the second item as the dictionary key and the first item as the value - value,key = line使用第二项作为字典键,第一项作为值 - value,key = line
    • add the value to that dictionary key - d[key] += value将值添加到该字典键 - d[key] += value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM