简体   繁体   English

Python csv.DictReader:解析字符串?

[英]Python csv.DictReader: parse string?

I am downloading a CSV file directly from a URL using requests . 我正在使用requests直接从URL下载CSV文件。

How can I parse the resulting string with csv.DictReader ? 如何用csv.DictReader解析生成的字符串?

Right now I have this: 现在我有这个:

r = requests.get(url)
reader_list = csv.DictReader(r.text)
print reader_list.fieldnames
for row in reader_list:
    print row

But I just get ['r'] as the result of fieldnames , and then all kinds of weird things from print row . 但我只是得到['r']作为fieldnames的结果,然后是print row各种奇怪的东西。

From documentation of csv , the first argument to csv.reader or csv.DictReader is csvfile - csv文档中, csv.readercsv.DictReader的第一个参数是csvfile -

csvfile can be any object which supports the iterator protocol and returns a string each time its next() method is called — file objects and list objects are both suitable csvfile可以是任何支持迭代器协议的对象,每次调用next()方法时都返回一个字符串 - 文件对象和列表对象都适用

In your case when you give the string as the direct input for the csv.DictReader() , the next() call on it only provides a single character, and hence that becomes the header, and then next() is continuously called to get each row. 在你将字符串作为csv.DictReader()的直接输入的情况next() ,对它的next()调用只提供一个字符,因此它成为标题,然后连续调用next()来获取每一行。

Hence, you need to either provide a in-memory stream of the string (Using StringIO) or a list of lines (using splitlines ) 因此,您需要提供字符串的内存中流(使用StringIO)或行列表(使用splitlines线)

You can use io.StringIO() and then use it in csv.DictReader . 您可以使用io.StringIO()然后在csv.DictReader使用它。 Example/Demo - 示例/演示 -

>>> import csv
>>> s = """a,b,c
... 1,2,3
... 4,5,6
... 7,8,9"""
>>> import io
>>> reader_list = csv.DictReader(io.StringIO(s))
>>> print reader_list.fieldnames
['a', 'b', 'c']
>>> for row in reader_list:
...     print row
... 
{'a': '1', 'c': '3', 'b': '2'}
{'a': '4', 'c': '6', 'b': '5'}
{'a': '7', 'c': '9', 'b': '8'}

Or as indicated in the comments , you can split the lines before giving as input to csv.DictReader . 或者如注释中所示,您可以在给csv.DictReader作为输入之前拆分行。 Example/Demo - 示例/演示 -

>>> reader_list = csv.DictReader(s.splitlines())
>>> print reader_list.fieldnames
['a', 'b', 'c']
>>> for row in reader_list:
...     print row
... 
{'a': '1', 'c': '3', 'b': '2'}
{'a': '4', 'c': '6', 'b': '5'}
{'a': '7', 'c': '9', 'b': '8'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM