我正在嘗試將 csv 文件作為 pandas 數(shù)據(jù)框?qū)?,其?csv 文件位于 zip 文件內(nèi)。為了高效導(dǎo)入,我嘗試先獲取標(biāo)題,然后再將其加載到 pandas 數(shù)據(jù)幀中。到目前為止我嘗試過的是:from zipfile import ZipFilefrom io import TextIOWrapperimport pandas as pdwith 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)問題是,當(dāng)我獲取next(reader)底層文件的標(biāo)頭受到影響時,該文件將作為沒有標(biāo)頭的 pandas 數(shù)據(jù)框?qū)搿U娴暮芨屑と魏涡迯?fù)。
1 回答

夢里花落0921
TA貢獻1772條經(jīng)驗 獲得超6個贊
您可以使用函數(shù)eek()重置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)
添加回答
舉報
0/150
提交
取消