python下载文件的几种常用方法

python中下载文件常用的几个模块有urllib,urllib2,requests,方法也很简单,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Python 2 code
import  urllib
import  urllib2
import  requests
               
url  =  'http://192.168.1.100/test.zip'
               
print  "downloading with urllib"
urllib.urlretrieve(url,  "code.zip" )
               
print  "downloading with urllib2"
=  urllib2.urlopen(url)
data  =  f.read()
with  open ( "code2.zip" "wb" ) as code:
     code.write(data)
               
print  "downloading with requests"
=  requests.get(url)
with  open ( "code3.zip" "wb" ) as code:
     code.write(r.content)


具体详情可参见:http://www.blog.pythonlibrary.org/2012/06/07/python-101-how-to-download-a-file/



本文转自 lover00751CTO博客,原文链接:http://blog.51cto.com/wangwei007/1351429,如需转载请自行联系原作者

} } }; // 矩阵加法运算符重载 Matrix operator+(double a, const Matrix& m) { Matrix result(m.rows, m.cols); for (int i = 0; i < m.rows; i++) { for (intPython有以下几种常见的写文件方法: 1. 使用内置的open函数和write方法,以文本模式打开文件并写入内容,例如: ```python with open('file.txt', 'w') as f: f.write j = 0; j < m.cols; j++) { result(i, j) = a + m(i, j); } ('Hello, world!') ``` 2. 使用内置的open函数和writelines方法,以文本模式打开文件 } return result; } // 矩阵减法运算符重载 Matrix operator-(double a, const Matrix& m)并写入多行内容,例如: ```python with open('file.txt', 'w') as f: f.writelines([' { Matrix result(m.rows, m.cols); for (int i = 0; i < m.rows; i++) { forHello\n', 'world\n']) ``` 3. 使用内置的open函数和dump方法,以二进制模式打开 (int j = 0; j < m.cols; j++) { result(i, j) = a - m(i, j); 文件并写入Python对象,例如: ```python import pickle data = {'name': 'Alice', 'age': 30} } } return result; } // 矩阵乘法运算符重载 Matrix operator*(double a, const Matrix&with open('file.pkl', 'wb') as f: pickle.dump(data, f) ``` 4. 使用第三方库, m) { Matrix result(m.rows, m.cols); for (int i = 0; i < m.rows; i++) { 例如pandas或csv,以特定格式写入数据到文件,例如: ```python import pandas as pd data = {' for (int j = 0; j < m.cols; j++) { result(i, j) = a * m(i,name': ['Alice', 'Bob'], 'age': [30, 40]} df = pd.DataFrame(data) df.to_csv('file.csv', index=False) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值