如何从CSV文件中获取值并通过列迭代
我的CSV文件示例:
'Date','Category','Ability'
'21,2,5','Sparrow','Air,land'
'4,5,6','Eagle','Air,Land'
我当前的代码:
with open(Filepath,'r') as f :
user_read=csv.reader(f)
dict_date=[line['Date'] for line in user_read]
print(dict_date)
错误:
typeerror:列表索引必须是整数或切片,而不是str
我的预期输出:
[21,2,5,4,5,6]
任何想法..所以我的类别和能力具有单独的字典
My csv File sample :
'Date','Category','Ability'
'21,2,5','Sparrow','Air,land'
'4,5,6','Eagle','Air,Land'
My current code:
with open(Filepath,'r') as f :
user_read=csv.reader(f)
dict_date=[line['Date'] for line in user_read]
print(dict_date)
Error :
TypeError : list indices must be integer or slices,not str
My expected Output :
[21,2,5,4,5,6]
Any ideas.. So my Category and Ability has seperate Dictionary
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试:
打印:
Try:
Prints:
您可以使用Pandas来完成它:
输出将为:
You can accomplice it using Pandas:
The output will be:
由于政策限制,我没有使用大熊猫
在对我有用的代码下方
I didnt use pandas due to policy restriction
Below the code which worked for me
您正在按dict访问行,可以通过
csv.dictreader
,而且您也没有设置
”。quodechar
对于您正在使用的方言,默认为>
。“以下方面确实可以做到这两个,并且以您想要的方式工作。
You're accessing the rows as dicts, which is available through
csv.DictReader
, and also you're not setting thequotechar
for the dialect you're using which therefore defaults to"
.The following does both and works in the way you want.