当前位置: 首页 > news >正文

对多个 CSV 文件做关联

【问题】
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('C:\csv1file.csv')
csv2=open('C:\csv2file.csv')
csv1reader=csv.reader(csv1)
csv2reader=csv.reader(csv2)
outputfile=open('C:\mapped.csv', 'wb')
csvwriter=csv.writer(outputfile)
counter=0
header1=csv1reader.next()
header2=csv2reader.next()
csvwriter.writerow(header2+header1[2:4])
for row1 in csv1reader:
 for row2 in csv2reader:
 if row1[0]==row2[0]:
 counter=counter+1
 csvwriter.writerow(row2+row1[2:4])

I am running this code in Python 2.7. As you might have guessed the two different results that I am getting are based on the indentation of the csvwriter statement in the above code. I feel I am quite close to the answer and understand the logic but somehow the loop doesn’t loop very well.

Can any one of you please assist?

Thanks.

别人给出的解法,有 bug 未解决

csvwriter.writerow(header2+header1[2:4])
csv2copy=[]
for row2 in csv2reader: csv2copy.append(row2)
for row1 in csv1reader:
 for row2 in csv2copy:
 print row1,row2,counter
 if row1[0]==row2[0]:
 counter=counter+1
 csvwriter.writerow(row2+row1[2:4])

【回答】

这是典型的左连接,python 要循环实现,比较麻烦。可以用 SPL 实现,代码简单易懂:

A
1=file("D:\\csv1.csv").import@t()
2=file("D:\\csv2.csv").import@t()
3>A2.switch(ID,A1:ID)
4=A2.new(ID.ID,Name,Service,Day,ID.Address,ID.Ph)
5=file("D:\\result.txt").export@t(A4)

A3:将 A2 中的 ID 切换为指引字段

A4:按关联记录获取最终结果

相关文章:

  • 记录一次数据库CPU被打满的排查过程
  • 金九银十准备季:Java IO流面试题(含答案)
  • Kubeadm方式快速搭建K8S集群1.20版本
  • 手把手开发Admin 系列一(设计篇)
  • java计算机毕业设计西安财经大学校园一卡通管理系统源码+系统+数据库+lw文档+mybatis+运行部署
  • 丁鹿学堂前端培训:前端性能优化css篇(一)
  • MASA Stack 第四期社区例会
  • 钓鱼 - deb 包篡改
  • Jackson关于Western Blot、IHC 和 ELISA 的显色检测分析
  • ES7+知识点整理使用
  • 一零二五、hexo : 无法加载文件 C:\Users\Tuomasi\AppData\Roaming\npm\hexo.ps1,因为在此系统上禁止运行脚本。
  • Android androidStudio构建项目过程中问题解决方案(持续更新)
  • windows10 使用 postgreSQL
  • NeuN抗体丨SYSY NeuN抗体说明书及相关研究工具
  • java 远程调用 httpclient 调用https接口
  • 【技术性】Search知识
  • Android开源项目规范总结
  • EventListener原理
  • exif信息对照
  • Git 使用集
  • iOS帅气加载动画、通知视图、红包助手、引导页、导航栏、朋友圈、小游戏等效果源码...
  • isset在php5.6-和php7.0+的一些差异
  • javascript 哈希表
  • js操作时间(持续更新)
  • mysql外键的使用
  • open-falcon 开发笔记(一):从零开始搭建虚拟服务器和监测环境
  • SpiderData 2019年2月25日 DApp数据排行榜
  • Twitter赢在开放,三年创造奇迹
  • 从输入URL到页面加载发生了什么
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 关于List、List?、ListObject的区别
  • 前端攻城师
  • 数据结构java版之冒泡排序及优化
  • 通信类
  • #《AI中文版》V3 第 1 章 概述
  • #我与Java虚拟机的故事#连载02:“小蓝”陪伴的日日夜夜
  • $.ajax()方法详解
  • (04)Hive的相关概念——order by 、sort by、distribute by 、cluster by
  • (10)Linux冯诺依曼结构操作系统的再次理解
  • (LeetCode C++)盛最多水的容器
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (汇总)os模块以及shutil模块对文件的操作
  • (九)One-Wire总线-DS18B20
  • (篇九)MySQL常用内置函数
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (原創) 博客園正式支援VHDL語法著色功能 (SOC) (VHDL)
  • (轉)JSON.stringify 语法实例讲解
  • .bashrc在哪里,alias妙用
  • .Family_物联网
  • .FileZilla的使用和主动模式被动模式介绍
  • .net core 调用c dll_用C++生成一个简单的DLL文件VS2008
  • .NET 使用配置文件
  • .NET 应用启用与禁用自动生成绑定重定向 (bindingRedirect),解决不同版本 dll 的依赖问题
  • .netcore 如何获取系统中所有session_ASP.NET Core如何解决分布式Session一致性问题