python读取csv文件列数,使用python 2.4计算CSV文件中的列数

I want to count the total number of columns in a CSV file. Currently i am using python 2.7 and 3.4. Code works perfectly in these versions and when i try to implement the same thing in python 2.4, it is showing as next() is not defined.

Code i am using currently(2.7 and 3.4)

f = open(sys.argv[1],'r')

reader = csv.reader(f,delimiter=d)

num_cols = len(next(reader)) # Read first line and count columns

My strong need is to implement the same in Python 2.4 . Any help would be greatly appreciated.

解决方案

I do not have Python 2.4 installed at the moment, so I can not really test this.

According to the documentation, the next builtin is new in Python 2.6. However, the csv.reader has a next method of it's own, and that one seems to have existed even in 2.4, so you should be able to use this.

num_cols = len(reader.next())

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值