【发布时间】:2011-10-25 21:41:29
【问题描述】:
我有一个 csv 文件,我想将数据解析到列表中。 所以我正在使用 python csv 模块来读取它 所以基本上如下:
import csv
fin = csv.reader(open(path,'rb'),delimiter=' ',quotechar='|')
print fin[0]
#gives the following
['"1239","2249.00","1","3","2011-02-20"']
#lets say i do the following
ele = str(fin[0])
ele = ele.strip().split(',')
print ele
#gives me following
['[\'"1239"', '"2249.00"', '"1"', '"3"', '"2011-02-20"\']']
现在 ele[0] 给我 --> 输出---> ['"1239"
我该如何摆脱那个 [' 最后,我想做的是得到 1239 并将其转换为整数..? 为什么会发生这种情况的任何线索 谢谢
编辑:*没关系..感谢第一条评论解决*
【问题讨论】:
-
为什么将分隔符设置为
' ',而这些实际上似乎是逗号分隔值?