从元组中提取信息(Python) [英] Extracting Information from a Tuple (Python)

查看:1119
本文介绍了从元组中提取信息(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Python 2.7中的httplib库从网站获取一些标题,以建立a)下载的文件大小和b)文件的最后修改日期。我已经使用了一些在线工具,这些细节确实存在。

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.

我目前正在编写Python代码脚本,它似乎正常工作,可以恢复所需的信息。尽管如此,包含标题信息的响应是包含多个元组的列表。回复样本如下: -

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')]

我要做的是基本上删除文件大小(2501479)和日期(星期四,2011年10月20日04:30:01 GMT)。我有什么想法可以做到这一点?我最初尝试变量[0] 但这会返回'content-length','2501479'。我怎样才能单独返回文件大小(理论上是列表中第一个元组的第二部分!)。

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

关于日期,我会使用 datetime 对象.python.org / library / email.util.html #email.utils.parsedaterel =noreferrer> email.utils.parsedate 功能:

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)

这篇关于从元组中提取信息(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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