如何在python中下载zip文件并从中解析csv文件

时间:2018-03-27 14:30:24

标签: python python-3.x

我写了一个脚本,点击URL并下载一个zip文件解压缩。现在我在解析解压缩后得到的CSV文件时遇到了问题。

import csv
from requests import get
from io import BytesIO
from zipfile import ZipFile

request = get('https://example.com/some_file.zip')
zip_file = ZipFile(BytesIO(request.content))
files = zip_file.namelist()
with open(files[0], 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    for row in csvreader:
        print(row)

3 个答案:

答案 0 :(得分:2)

执行files = zip_file.namelist()时,只需列出zip存档中文件的名称;这些文件尚未从zip中提取,您不能open将它们作为本地文件,就像您正在做的那样。

您可以使用ZipFile.open直接从zip文件中读取数据流。

所以这应该有效:

zip_file = ZipFile(BytesIO(request.content))
files = zip_file.namelist()
with zip_file.open(files[0], 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    ...

答案 1 :(得分:0)

您似乎尚未导入csv模块。尝试将导入import csv置于顶部。

答案 2 :(得分:0)

response = requests.get(url)
with io.BytesIO(response.content) as zip_file:
    with zipfile.ZipFile() as zip_file:
        # Get first file in the archive
        for zip_info in zip_file.infolist():
            logger.debug(zip_info)
            # Open file
            with zip_file.open(zip_info) as file:
                # Load CSV file, decode binary to text
                with io.TextIOWrapper(file) as text:
                    return csv.DictReader(text)