在Python中从网址下载csv.gz文件 [英] Downloading a csv.gz file from url in Python

查看:214
本文介绍了在Python中从网址下载csv.gz文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从url下载csv.gz文件,我也没有下载tar.gz文件的问题.对于csv.gz文件,我可以提取.gz文件并读取我的csv文件,如果我可以使用URL而不是先使用csv-1.0.csv.gz,那将非常方便

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

这有效:

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

这不起作用:

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

我收到此错误:UnicodeEncodeError:'ascii'编解码器无法在位置9编码字符'\ xad':序数不在范围内(128)

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

推荐答案

requests 模块用于更高级别的http客户端界面.该代码非常简单:

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)

基本上,在分配了URL和目标文件名之后,您将打开目标文件以二进制模式进行写入,请求该文件,然后将请求的内容写入该文件.完成并完成.

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.

这篇关于在Python中从网址下载csv.gz文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆