通过公共列组合2个.csv文件 [英] Combining 2 .csv files by common column

查看:111
本文介绍了通过公共列组合2个.csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有两个.csv文件,其中文件1的第一行是:

  MPID,标题,说明,模型,类别ID,类别描述,子类别ID,子类别描述,制造商ID,制造商描述,URL,制造商(品牌)URL,图像URL,AR价格,价格,船舶价格,库存,条件

档案2的第一行:

 正常价格,销售价格,制造商名称,型号,零售商类别,购买网址,产品名称,可用性,运费,条件,MPID,图像URL,UPC,说明
pre>

,然后每个文件的其余部分填充信息。



正如你可以看到,一个称为MPID的公共字段(文件1:col 1,文件2:col 9,其中第一个col是col 1)。



我想创建一个新文件它将通过查看此列(如:如果两个文件中都有一个MPID来组合这两个文件,则在新文件中,此MPID将同时显示其从文件1的行和从文件2的行)。如果一个MPID只出现在一个文件,那么它也应该进入这个组合文件。



文件不以任何方式排序。



如何在debian机器上一个shell脚本或python?



谢谢。



编辑:两个文件除了分隔符田野。

解决方案

  sort -t,-k index1 file1& sorted1 
sort -t,-k index2 file2> sorted2
join -t,-1 index1 -2 index2 -a 1 -a 2 sorted1 sorted2


So I have two .csv files where the first line in file 1 is:

MPID,Title,Description,Model,Category ID,Category Description,Subcategory ID,Subcategory Description,Manufacturer ID,Manufacturer Description,URL,Manufacturer (Brand) URL,Image URL,AR Price,Price,Ship Price,Stock,Condition

The first line from file 2:

Regular Price,Sale Price,Manufacturer Name,Model Number,Retailer Category,Buy URL,Product Name,Availability,Shipping Cost,Condition,MPID,Image URL,UPC,Description

and then rest of every file is filled with info.

As you can see, both files have a common field called MPID (file 1: col 1, file 2: col 9, where the first col is col 1).

I would like to create a new file which will combine these two files by looking at this column (as in: if there is an MPID that is in both files, then in the new file this MPID will appear with both its row from file 1 and its row from file 2). IF one MPID appears only in one file then it should also go into this combined file.

The files are not sorted in any way.

How do I do this on a debian machine with either a shell script or python?

Thanks.

EDIT: Both files dont have commas other than the ones separating the fields.

解决方案

sort -t , -k index1 file1 > sorted1
sort -t , -k index2 file2 > sorted2
join -t , -1 index1 -2 index2 -a 1 -a 2 sorted1 sorted2

这篇关于通过公共列组合2个.csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆