从压缩文件中读取CSV

2024-05-16 11:49:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从压缩文件中读取所有csv,但压缩文件中存在多个csv。附加的压缩文件URL。任何人回答都将不胜感激。 我尝试过这个,但得到一个错误,即在压缩文件中有多个CSV文件

df = pd.read_csv('http://dados.cvm.gov.br/dados/FIDC/DOC/INF_MENSAL/DADOS/inf_mensal_fidc_202006.zip', compression='zip', header=1, sep=';', quotechar='"')
print(df)

Tags: 文件csvbrhttpurldfread错误
1条回答
网友
1楼 · 发布于 2024-05-16 11:49:40
import urllib.request
import zipfile
import pandas as pd

#first you unzip it
url = 'http://dados.cvm.gov.br/dados/FIDC/DOC/INF_MENSAL/DADOS/inf_mensal_fidc_202006.zip'
file_name = "dados.zip"
path = os.getcwd()
destination = path + "\\" + file_name
urllib.request.urlretrieve(url, destination)

#second you extract evrything from the zip into your folder destination
directory_name = "dados_folder"
zip_destination = path + "\\" + directory_name
os.mkdir(zip_destination)
with zipfile.ZipFile(destination, 'r') as zip_ref:
    zip_ref.extractall(zip_destination)
    
#now you read each csv one by one and put it into a dataframe
roman_numerals = ["I", "II", "III", "IV", "IX", "V", "VI", "VII", "X_1", "X_2", "X_3", "X_4", "X_5", "X_6", "X_7", "X_1_1"]
for x in roman_numerals:
    name_csv = path + "\\" + "inf_mensal_fidc_tab_" + x + "_202006.csv"
    with open(name_csv, "r+") as f: # no need to close it as by using with it closes by itself
        df = read_csv(name_csv)

相关问题 更多 >