![](/img/trans.png)
[英]How to Load a CSV File from zipped folder from URL into Pandas DataFrame
[英]Getting headers from zipped csv file before importing as pandas dataframe
我正在嘗試將 csv 文件導入為 pandas dataframe,其中 csv 文件位於 zip 文件中。 為了高效導入,我試圖在將其加載到 pandas dataframe 之前先獲取標頭。
到目前為止我嘗試的是:
from zipfile import ZipFile
from io import TextIOWrapper
import pandas as pd
with ZipFile(zip_path, 'r') as zipfile:
with zipfile.open(file_path, 'r') as file:
reader = csv.reader(TextIOWrapper(file, 'utf-8', newline=''))
headers = next(reader)
df = pd.read_csv(file)
問題是當我使用next(reader)
獲取標頭時,基礎文件會受到影響,並且文件將導入為 pandas dataframe 沒有標頭。
真的很感激任何修復。
您可以使用 function seek() 重置 CSV 迭代器:
with ZipFile('test.zip', 'r') as zipfile:
with zipfile.open('test.csv', 'r') as file:
reader = csv.reader(TextIOWrapper(file, 'utf-8', newline=''))
headers = next(reader)
# reset CSV iterator
file.seek(0)
df = pd.read_csv(file)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.