python 元组,从元组中提取信息(Python)

I'm currently using the httplib library in Python 2.7 to obtain some headers from a website to establish a) the filesize of a download and b) the last modified date of the file. I've used some online tools and these details do exist.

I'm currently scripting my Python code and it appears to work correctly bringing back the required information. Nonetheless, the response containing the header information is a list containing a number of tuples. A sample of the response is below:-

[('content-length', '2501479'),

('accept-ranges', 'bytes'),

('vary', 'Accept-Encoding'),

('server', 'off'),

('last-modified', 'Thu, 20 Oct 2011 04:30:01 GMT'),

('etag', '"2c8171a-262b67-4afb368edfffc"'),

('date', 'Thu, 20 Oct 2011 16:01:11 GMT'),

('content-type', 'text/plain')]

What I am looking to do is strip out basically the file size ("2501479") and the date ("Thu, 20 Oct 2011 04:30:01 GMT"). Any ideas how I can go about doing this? I originally tried variable[0] but this returns "'content-length', '2501479'". How can I return the filesize solely (in theory the second part of the first tuple in the list!).

解决方案

First, you can make it a little easier to work with by turning your list of tuples into a dictionary:

>>> headers = [('content-length', '2501479'),

... ('accept-ranges', 'bytes'),

... ('vary', 'Accept-Encoding'),

... ('server', 'off'),

... ('last-modified', 'Thu, 20 Oct 2011 04:30:01 GMT'),

... ('etag', '"2c8171a-262b67-4afb368edfffc"'),

... ('date', 'Thu, 20 Oct 2011 16:01:11 GMT'),

... ('content-type', 'text/plain')]

>>>

>>> headers = dict(headers)

>>> int(headers['content-length'])

2501479

For the date, I would turn it into a datetime object using the email.utils.parsedate function:

>>> import email.utils

>>> email.utils.parsedate(headers['date'])

(2011, 10, 20, 16, 1, 11, 0, 1, -1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值