0
  1. 我有python3.3
  2. 我有一个来自 urllib3 的 Response 对象(它有一个stream()返回生成器的方法)
  3. 我有一个要写入数据的文件

将数据从(1)写入(2)的最惯用的方法是什么?我可以使用列表理解来执行以下操作:

with http.request_encode_url('GET', …, {'param1': value1}) as response:
    with open(path, 'wb') as fp:
        [fp.write(piece) for piece in response.stream(decode_content=True)]

但是对于这样一个常见的操作来说,它看起来太手动了

4

2 回答 2

1

The list comprehension is not idiomatic because those are meant to produce lists. Meaningful ones. Not ones that are never used and just created for side effects.

Simply use the writelines() method of file objects:

with http.request_encode_url('GET', …, {'param1': value1}) as response:
    with open(path, 'wb') as fp:
        fp.writelines(response.stream(decode_content=True))
于 2014-07-24T15:45:37.093 回答
1

The proper solution looks like this (thanks for the hint, @Blackjack):

from shutil import copyfileobj
from urllib3 import HTTPConnectionPool

_http = HTTPConnectionPool(host='www.example.org', port=80)

response = _http.request_encode_url(
    'GET', 'http://www.example.org/',
    {'param1': value1},
    preload_content=False, decode_content=True
)

with open('output.html', 'wb') as fp:
    copyfileobj(response, fp)

Warning: preload_content=False is important. otherwise, response would return empty byte-string

于 2014-07-25T10:42:37.613 回答