英文:
Is it possible to read and parse csv in streaming mode with Python?
问题
我想要下载一个庞大的CSV文件,然后以流模式逐行解析它。我的代码是:
with httpx.stream("GET", url) as r:
for line in r.iter_lines():
for row in csv.reader():
...
但如果输入文件中有"多行",这段代码就无法正常工作。
col11,col12,col13
col21,"co
l22", col23
你有解决办法吗?
英文:
I want to download huge CSV and parse it line by line in streaming mode. My code is:
with httpx.stream("GET", url) as r:
for line in r.iter_lines():
for row in csv.reader():
...
but if there are "multi-lines" in input file this code doesn't work.
col11,col12,col13
col21,"co
l22", col23
Do you have any idea how to solve this?
答案1
得分: 6
r.iter_lines()
是文本行的迭代器,就像一个文件一样。
所以如果你可以将文件传递给 csv.reader
,你也可以传递 r.iter_lines()
。
with httpx.stream("GET", url) as r:
for row in csv.reader(r.iter_lines()):
...
英文:
r.iter_lines()
is an iterator over lines of text, just as a file is.
So if you can pass a file to csv.reader
, you can pass r.iter_lines()
as well.
with httpx.stream("GET", url) as r:
for row in csv.reader(r.iter_lines()):
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论