Downloading a csv.gz file from url in Python

你说的曾经没有我的故事 提交于 2020-05-13 14:02:11

问题


I'm having trouble downloading a csv.gz file from a url I have no problem downloading a tar.gz file. For the csv.gz file I'm able to extract the .gz file and read my csv file it would just be handy if I could use an URL instead of having the csv-1.0.csv.gz before hand

This works:

import urllib.request
urllib.request.urlretrieve('http://www.mywebsite.com/csv-1-0.tar.gz','csv-1-0.tar.gz')

This does not work:

import urllib.request
urllib.request.urlretrieve('http://www.mywebsite.com/csv-1-0.csv.gz','csv-1-0.csv.gz')

I get this error: UnicodeEncodeError: 'ascii' codec can't encode character '\xad' in position 9: ordinal not in range(128)


回答1:


As suggested at the very beginning of the docs for urllib.request, the excellent requests module is recommended for higher-level http client interfaces. The code is quite straightforward:

import requests

url = "http://www.mywebsite.com/csv-1-0.csv.gz"
filename = url.split("/")[-1]
with open(filename, "wb") as f:
    r = requests.get(url)
    f.write(r.content)

Basically, after assigning the URL and the destination file name, you open the destination file for writing in binary mode, request the file, then write the content of the request to the file. Done and done.



来源:https://stackoverflow.com/questions/34561746/downloading-a-csv-gz-file-from-url-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!