reader(...)
csv_reader = reader(iterable [, dialect='excel']
[optional keyword args])
for row in csv_reader:
process(row)
The "iterable" argument can be any object that returns a line
of input for each iteration, such as a file object or a list. The
optional "dialect" parameter is discussed below. The function
also accepts optional keyword arguments which override settings
provided by the dialect.
所以如果你有弦:
>>> s = '"this is", "a test", "of the csv", "parser"'
您需要“一个对象,它为每个
“迭代”,您只需将字符串包装在列表中:
>>> r = csv.reader([s])
>>> list(r)
[['this is', 'a test', 'of the csv parser']]
您仍然可以使用
csv
解析单个字符串。使用StringIO编写字符串buffer(也称为内存文件):仔细查看
csv
模块的文档,其中 说:所以如果你有弦:
您需要“一个对象,它为每个 “迭代”,您只需将字符串包装在列表中:
这就是用
csv
模块解析字符串的方法。基本上只需@larsks回答上面的问题,但更简短的是,它演示了它在csv值上的工作,这些值在引号中有逗号。
如果你投票给我,也要投票给另一个答案。https://stackoverflow.com/a/35822856/1196339
相关问题 更多 >
编程相关推荐