python处理csv文件列错位,Python:比较两个csv文件中的特定列

Say that I have two CSV files (file1 and file2) with contents as shown below:

file1:

fred,43,Male,"23,45",blue,"1, bedrock avenue"

file2:

fred,39,Male,"23,45",blue,"1, bedrock avenue"

I would like to compare these two CSV records to see if columns 0,2,3,4, and 5 are the same. I don't care about column 1.

What's the most pythonic way of doing this?

EDIT:

Some example code would be appreciated.

EDIT2:

Please note the embedded commas need to be handled correctly.

解决方案

I suppose the best ways is to use Python library: http://docs.python.org/library/csv.html.

UPDATE (example added):

import csv

reader1 = csv.reader(open('data1.csv', 'rb'), delimiter=',', quotechar='"'))

row1 = reader1.next()

reader2 = csv.reader(open('data2.csv', 'rb'), delimiter=',', quotechar='"'))

row2 = reader2.next()

if (row1[0] == row2[0]) and (row1[2:] == row2[2:]):

print "eq"

else:

print "different"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值