对多个 CSV 文件做关联

博主尝试将两个 CSV 文件根据 ID 进行匹配,将 CSV1 的 Address 和 Ph 字段添加到 CSV2 中并保存为新的输出文件。遇到的问题是所有 CSV1 记录都与 CSV2 的最后一行匹配,或者所有 CSV2 记录被赋予相同的地址信息。目前在 Python 2.7 环境下编写了代码,但逻辑存在循环问题。寻求解决方案。
摘要由CSDN通过智能技术生成

【问题】
I have two CSV files as follows:

CSV1:

**ID Name Address Ph**
 1 Mr.C dsf 142
 2 Ms.N asd 251
 4 Mr.V fgg 014
 12 Ms.S trw 547

CSV2:

 **ID Name Service Day**
1 Mr.C AAA Mon
 2 Ms.N AAA Mon
 2 Ms.N BBB Tue
 2 Ms.N AAA Sat

As you can see very quickly CSV1 file is unique in having only 1 instance of every ID whilst CSV2 has repeats.

I am trying to match two CSV files based on ID and then wherever they match adding to CSV2 file the Address and Ph fields from CSV1. This is then saved as a new output file leaving the two original CSV files intact.

I have written a code but here’s what’s happening:

  1. Either all the entries from CSV1 get added against the last row of CSV2

  2. Or all the entries from CSV2 get the same address details appended against them

Here’s what I have done so far.

import csv
csv1=open(&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值