[英]Open file from zip without extracting it in Python?
我正在編寫一個腳本,使用tje請求庫從URL中獲取zip文件。 該zip文件包含csv文件。 我試圖讀取該csv文件而不保存它。 但是解析它時給了我這個錯誤: _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
import csv
import requests
from io import BytesIO, StringIO
from zipfile import ZipFile
response = requests.get(url)
zip_file = ZipFile(BytesIO(response.content))
files = zip_file.namelist()
with zip_file.open(files[0]) as csvfile:
csvreader = csv.reader(csvfile)
# _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
for row in csvreader:
print(row)
試試這個:
import pandas as pd
import requests
from io import BytesIO, StringIO
from zipfile import ZipFile
response = requests.get(url)
zip_file = ZipFile(BytesIO(response.content))
files = zip_file.namelist()
with zip_file.open(files[0]) as csvfile:
print(pd.read_csv(csvfile, encoding='utf8', sep=","))
正如@ Aran-Fey提到的那樣:
import zipfile
import csv
import io
with open('/path/to/archive.zip', 'r') as f:
with zipfile.ZipFile(f) as zf:
csv_filename = zf.namelist()[0] # see namelist() for the list of files in the archive
with zf.open(csv_filename) as csv_f:
csv_f_as_text = io.TextIOWrapper(csv_f)
reader = csv.reader(csv_f_as_text)
csv.reader
(和csv.DictReader
)需要在文本模式下打開類似文件的對象。 通常,在'r'
模式下open(...)
文件時這不是問題,正如Python 3文檔所說,文本模式是默認模式:“默認模式是'r'(打開用於閱讀文本,同義詞'rt')“。 但是如果你在ZipFile
上嘗試使用open
rt
,你會看到一個錯誤: ZipFile.open() requires mode "r" or "w"
:
with zf.open(csv_filename, 'rt') as csv_f:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
...
ValueError: open() requires mode "r" or "w"
這就是io.TextIOWrapper
的用途 - 用於將字節流包裝為文本可讀,並在運行時解碼它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.