是可能以Python的流式模式读取和解析CSV文件吗?

huangapple go评论64阅读模式
英文:

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()):
        ...

huangapple
  • 本文由 发表于 2023年5月30日 00:35:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定