在元组列表中搜索

2024-09-27 00:19:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我提取数据集行:

(u"[('x-xss-protection', '1; mode=block'), ('x-powered-by', 'PHP/5.4.21 ZendServer/6.2.0'), ('transfer-encoding', 'chunked'), ('expires', '0'), ('vary', 'Accept-Encoding'), ('server', 'nginx'), ('last-modified', 'Thu, 03 Dec 2015 10:20:02 GMT'), ('connection', 'keep-alive'), ('pragma', 'no-cache'), ('cache-control', 'must-revalidate, post-check=0, pre-check=0, public'), ('date', 'Thu, 03 Dec 2015 10:20:02 GMT'), ('x-cached', 'EXPIRED'), ('content-type', 'text/html')]",)

我想找到“内容类型”。 我试过dict,list,enumerate,str。。。 但什么都不管用:(


Tags: 数据cachebymodecheckblockdectransfer
1条回答
网友
1楼 · 发布于 2024-09-27 00:19:04

元组中的字符串看起来像列表,我建议使用^{}将其转换为实列表,然后将其转换为dict(只需运行dict()函数),并使用'content-type'作为访问'text/html'(值)的键:

import ast
t = (u"[('x-xss-protection', '1; mode=block'), ('x-powered-by', 'PHP/5.4.21 ZendServer/6.2.0'), ('transfer-encoding', 'chunked'), ('expi res', '0'), ('vary', 'Accept-Encoding'), ('server', 'nginx'), ('last-modified', 'Thu, 03 Dec 2015 10:20:02 GMT'), ('connection', 'keep-alive '), ('pragma', 'no-cache'), ('cache-control', 'must-revalidate, post-check=0, pre-check=0, public'), ('date', 'Thu, 03 Dec 2015 10:20:02 GMT '), ('x-cached', 'EXPIRED'), ('content-type', 'text/html')]",)
dict(ast.literal_eval(t[0]))['content-type']

输出:

'text/html'

相关问题 更多 >

    热门问题