如何在python中访问字典内的字典值

时间:2018-04-12 00:57:11

标签: python dictionary

我想访问" commonName"的值在这个脚本中: 输出生成错误:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    print(x["subject"]["commonName"])
TypeError: tuple indices must be integers or slices, not str

我可以访问x["subject"],但这不是我想要的。我想要commonName值,但无法访问它。

x = {'subject': ((('countryName', 'US'),), (('stateOrProvinceName', 'California'),), (('localityName', 'Mountain View'),), (('organizationName', 'Google Inc'),), (('commonName', '*.google.com'),)), 'issuer': ((('countryName', 'US'),), (('organizationName', 'Google Inc'),), (('commonName', 'Google Internet Authority G2'),)), 'version': 3, 'serialNumber': '297B797A0530FA58', 'notBefore': 'Mar 28 13:56:32 2018 GMT', 'notAfter': 'Jun 20 13:23:00 2018 GMT', 'subjectAltName': (('DNS', '*.google.com'), ('DNS', '*.android.com'), ('DNS', '*.appengine.google.com'), ('DNS', '*.cloud.google.com'), ('DNS', '*.db833953.google.cn'), ('DNS', '*.g.co'), ('DNS', '*.gcp.gvt2.com'), ('DNS', '*.google-analytics.com'), ('DNS', '*.google.ca'), ('DNS', '*.google.cl'), ('DNS', '*.google.co.in'), ('DNS', '*.google.co.jp'), ('DNS', '*.google.co.uk'), ('DNS', '*.google.com.ar'), ('DNS', '*.google.com.au'), ('DNS', '*.google.com.br'), ('DNS', '*.google.com.co'), ('DNS', '*.google.com.mx'), ('DNS', '*.google.com.tr'), ('DNS', '*.google.com.vn'), ('DNS', '*.google.de'), ('DNS', '*.google.es'), ('DNS', '*.google.fr'), ('DNS', '*.google.hu'), ('DNS', '*.google.it'), ('DNS', '*.google.nl'), ('DNS', '*.google.pl'), ('DNS', '*.google.pt'), ('DNS', '*.googleadapis.com'), ('DNS', '*.googleapis.cn'), ('DNS', '*.googlecommerce.com'), ('DNS', '*.googlevideo.com'), ('DNS', '*.gstatic.cn'), ('DNS', '*.gstatic.com'), ('DNS', '*.gvt1.com'), ('DNS', '*.gvt2.com'), ('DNS', '*.metric.gstatic.com'), ('DNS', '*.urchin.com'), ('DNS', '*.url.google.com'), ('DNS', '*.youtube-nocookie.com'), ('DNS', '*.youtube.com'), ('DNS', '*.youtubeeducation.com'), ('DNS', '*.yt.be'), ('DNS', '*.ytimg.com'), ('DNS', 'android.clients.google.com'), ('DNS', 'android.com'), ('DNS', 'developer.android.google.cn'), ('DNS', 'developers.android.google.cn'), ('DNS', 'g.co'), ('DNS', 'goo.gl'), ('DNS', 'google-analytics.com'), ('DNS', 'google.com'), ('DNS', 'googlecommerce.com'), ('DNS', 'source.android.google.cn'), ('DNS', 'urchin.com'), ('DNS', 'www.goo.gl'), ('DNS', 'youtu.be'), ('DNS', 'youtube.com'), ('DNS', 'youtubeeducation.com'), ('DNS', 'yt.be')), 'OCSP': ('http://clients1.google.com/ocsp',), 'caIssuers': ('http://pki.google.com/GIAG2.crt',), 'crlDistributionPoints': ('http://pki.google.com/GIAG2.crl',)}

print(x["subject"]["commonName"])

3 个答案:

答案 0 :(得分:1)

你要找的是一个元组,而不是一个字典。您可以通过使用元组展平项目并将每个对转换为键/值并按键查找来将其转换为dict。

使用itertools.chain展平:

In [35]: import itertools

In [36]: dict(itertools.chain(*x['subject']))['commonName']
Out[36]: '*.google.com'

或在展平后使用next,并匹配名称:

In [38]: next(val for name, val in itertools.chain(*x['subject']) if name == 'commonName')
Out[38]: '*.google.com'

答案 1 :(得分:0)

x = {'subject': ((('countryName', 'US'),), (('stateOrProvinceName', 'California'),), (('localityName', 'Mountain View'),), (('organizationName', 'Google Inc'),), (('commonName', '*.google.com'),)), 'issuer': ((('countryName', 'US'),), (('organizationName', 'Google Inc'),), (('commonName', 'Google Internet Authority G2'),)), 'version': 3, 'serialNumber': '297B797A0530FA58', 'notBefore': 'Mar 28 13:56:32 2018 GMT', 'notAfter': 'Jun 20 13:23:00 2018 GMT', 'subjectAltName': (('DNS', '*.google.com'), ('DNS', '*.android.com'), ('DNS', '*.appengine.google.com'), ('DNS', '*.cloud.google.com'), ('DNS', '*.db833953.google.cn'), ('DNS', '*.g.co'), ('DNS', '*.gcp.gvt2.com'), ('DNS', '*.google-analytics.com'), ('DNS', '*.google.ca'), ('DNS', '*.google.cl'), ('DNS', '*.google.co.in'), ('DNS', '*.google.co.jp'), ('DNS', '*.google.co.uk'), ('DNS', '*.google.com.ar'), ('DNS', '*.google.com.au'), ('DNS', '*.google.com.br'), ('DNS', '*.google.com.co'), ('DNS', '*.google.com.mx'), ('DNS', '*.google.com.tr'), ('DNS', '*.google.com.vn'), ('DNS', '*.google.de'), ('DNS', '*.google.es'), ('DNS', '*.google.fr'), ('DNS', '*.google.hu'), ('DNS', '*.google.it'), ('DNS', '*.google.nl'), ('DNS', '*.google.pl'), ('DNS', '*.google.pt'), ('DNS', '*.googleadapis.com'), ('DNS', '*.googleapis.cn'), ('DNS', '*.googlecommerce.com'), ('DNS', '*.googlevideo.com'), ('DNS', '*.gstatic.cn'), ('DNS', '*.gstatic.com'), ('DNS', '*.gvt1.com'), ('DNS', '*.gvt2.com'), ('DNS', '*.metric.gstatic.com'), ('DNS', '*.urchin.com'), ('DNS', '*.url.google.com'), ('DNS', '*.youtube-nocookie.com'), ('DNS', '*.youtube.com'), ('DNS', '*.youtubeeducation.com'), ('DNS', '*.yt.be'), ('DNS', '*.ytimg.com'), ('DNS', 'android.clients.google.com'), ('DNS', 'android.com'), ('DNS', 'developer.android.google.cn'), ('DNS', 'developers.android.google.cn'), ('DNS', 'g.co'), ('DNS', 'goo.gl'), ('DNS', 'google-analytics.com'), ('DNS', 'google.com'), ('DNS', 'googlecommerce.com'), ('DNS', 'source.android.google.cn'), ('DNS', 'urchin.com'), ('DNS', 'www.goo.gl'), ('DNS', 'youtu.be'), ('DNS', 'youtube.com'), ('DNS', 'youtubeeducation.com'), ('DNS', 'yt.be')), 'OCSP': ('http://clients1.google.com/ocsp',), 'caIssuers': ('http://pki.google.com/GIAG2.crt',), 'crlDistributionPoints': ('http://pki.google.com/GIAG2.crl',)}
# ^_^ 
print(x["subject"][0][0])# type --> tuple('countryName', 'US')
print(dict(x["subject"][0])["countryName"]) 
print(dict(x["subject"][0]).get("countryName"))

答案 2 :(得分:0)

有一点需要注意,我建议小心,因为这个数据结构表明嵌套元组可能有重复的对:

>>> dict(map(lambda x: x[0], x['subject']))['commonName']
'*.google.com'

诀窍。