Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Pandadev
Post Prodigy
Post Prodigy

Import a zipped csv file from web url

Hi . I have been trying to import a csv file direct from a web url , which is zipped.

I have attempted to do this with python. but it just seems to run for ever.

Is my code incorrect , or is there a simpler solution please

from io import BytesIO
from zipfile import ZipFile
import pandas
import requests

url = "https://www.anac.gov.br/assuntos/dados-e-estatisticas/dados-estatisticos/arquivos/Dados_Estatisticos..."
content = requests.get(url)
zf = ZipFile(BytesIO(content.content))

for item in zf.namelist():
print("File in zip: "+ item)

# find the first matching csv file in the zip:
match = [s for s in zf.namelist() if "Dados" in s][0]
# the first line of the file contains a string - that line shall de ignored, hence skiprows
df = pandas.read_csv(zf.open(match),encoding='latin-1', error_bad_lines=False)
print(df)

1 ACCEPTED SOLUTION
v-lionel-msft
Community Support
Community Support

Hello @Pandadev ,

Try these codes.

Repackaged the url.

from io import BytesIO
from zipfile import ZipFile
import pandas
import requests

url = "https://www.anac.gov.br/assuntos/dados-e-estatisticas/dados-estatisticos/arquivos/Dados_Estatisticos.zip/@@download/file/Dados%20Estat%C3%ADsticos.zip"
filename = requests.get(url).content
zf = ZipFile( BytesIO(filename), 'r' )
for item in zf.namelist():
    print("File in zip: "+ item)

match = [s for s in zf.namelist() if "Dados" in s][0]
df = pandas.read_csv( zf.open(match), encoding='latin-1', error_bad_lines=False)
print(df)

link.PNG

Best regards
Lionel Chen

If this post helps,then consider Accepting it as the solution to help other members find it more quickly.

View solution in original post

4 REPLIES 4
v-lionel-msft
Community Support
Community Support

Hello @Pandadev ,

Try these codes.

Repackaged the url.

from io import BytesIO
from zipfile import ZipFile
import pandas
import requests

url = "https://www.anac.gov.br/assuntos/dados-e-estatisticas/dados-estatisticos/arquivos/Dados_Estatisticos.zip/@@download/file/Dados%20Estat%C3%ADsticos.zip"
filename = requests.get(url).content
zf = ZipFile( BytesIO(filename), 'r' )
for item in zf.namelist():
    print("File in zip: "+ item)

match = [s for s in zf.namelist() if "Dados" in s][0]
df = pandas.read_csv( zf.open(match), encoding='latin-1', error_bad_lines=False)
print(df)

link.PNG

Best regards
Lionel Chen

If this post helps,then consider Accepting it as the solution to help other members find it more quickly.

When I run the python script in PowerBI it does not work , but when I run it direct in Python it works , any ideas why this is the case.

Thanks that worked perfect

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.