python csv问题 [英] python csv question

查看:185
本文介绍了python csv问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是测试出csv组件在python,我有一些麻烦。

i'm just testing out the csv component in python, and i am having some trouble with it.

我有一个相当标准的csv字符串,默认选项似乎都符合我的测试,但结果不应该分组1,2,3,4

I have a fairly standard csv string, and the default options all seems to fit with my test, but the result shouldn't group 1, 2, 3, 4 in a row and 5, 6, 7, 8 in a row?

非常感谢您提供的任何启发。

Thanks a lot for any enlightenment provided!


Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39) 
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>> c = "1, 2, 3, 4\n 5, 6, 7, 8\n"
>>> test = csv.reader(c)
>>> for t in test:
...     print t
... 
['1']
['', '']
[' ']
['2']
['', '']
[' ']
['3']
['', '']
[' ']
['4']
[]
[' ']
['5']
['', '']
[' ']
['6']
['', '']
[' ']
['7']
['', '']
[' ']
['8']
[]
>>> 


推荐答案

csv.reader期望可迭代。你给它1,2,3,4 \ n 5,6,7,8 \\\
;迭代产生字符。尝试给它[1,2,3,4 \\\
,5,6,7,8 \\\
] - 迭代将生成行。

csv.reader expects an iterable. You gave it "1, 2, 3, 4\n 5, 6, 7, 8\n"; iteration produces characters. Try giving it ["1, 2, 3, 4\n", "5, 6, 7, 8\n"] -- iteration will produce lines.

这篇关于python csv问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆