python的reader函数读取的信息包括抬头嘛,使用Python DictReader获取特定的行和值

I have a csv file and am trying to get one specific value on, say, line 20, column 3.

But so far, all I managed is to display all values on column 3 (here called "name").

Here is my Python code

d = DictReader(r.csv().split('\n'))

for line in d:

score = line["name"]

print score

How can I display and get the value for only a specific line?

解决方案

Naive solution:

target_row = 5

for num, line in enumerate(d):

if num == target_row:

print line["name"]

break

I removed the intermediate variable score.

Note that this is non-optimal, as you iterate until you reach the desired row, but I don't know if there is random access to lines in the DictReader.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值