Extracting Information from a Tuple (Python)

℡╲_俬逩灬. 提交于 2019-12-05 08:54:06

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)

First, convert the tuples into a dict, and then convert the value to int to get a number:

response_tupels = [('content-length', '2501479'), ('accept-ranges', 'bytes'),]
response = dict(response_tupels)
try:
  content_length = int(response['content-length'])
except KeyError:
  raise # Handle missing content-length here

You simply have to index it again in order to access the tuple. Like

length = variable[0][1]
last_mod = variable[4][1]

for size and the date of last modification.

Note: This only works when the indices of content-length and last-modified are always the same.

You've got tuples inside an array... Luckily you can reference (or dereference them depending on your terminology) the same way...

so v = x[0] will give you as you state the tuple ("'content-length', '2501479'") and v[0] will give you 'content-length' and v[1] will give you '2501479' (although you probably want to do an int(v[0]) on that with perhaps some error checking.

You may be better putting that array into a dict though; so you can be certain you are getting out the content length if the order should ever change.

Thankfully, the syntax is almost the same - it uses the [] operator. However, I am going to leave it to you to look at the python man pages to see how to convert an array -> dict (can't do everything for you!!)

mas = [('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')]
mas = dict(mas)
mas.get('content-length')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!