首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python json库只检索根元素。

Python json库只检索根元素。
EN

Stack Overflow用户
提问于 2022-02-18 23:37:21
回答 2查看 51关注 0票数 0

我很难使用json库从长文本中检索json数据。数据通过curl (text.txt)从cisco搜索工具api中检索。

我的代码只识别根元素。子元素不被检索。

我不知道我错过了什么。

代码:

代码语言:javascript
复制
with open('text.txt', 'r', encoding='utf8', errors='ignore') as file:
    filecontent = file.readlines()
for line in filecontent:
    if re.search("^\{\"advisories\"(.*?)\}", line):
        y = json.loads(line)
    for key, value in y.items():
        print("Key:" + key)

输出

代码语言:javascript
复制
root@server#python3 security.py
Key:advisories
root@server#

text.txt

代码语言:javascript
复制
{"advisories":[{"advisoryId":"cisco-sa-fxo-pattern-bypass-jUXgygYv","advisoryTitle":"Cisco IOS and IOS XE Software FXO Interface Destination Pattern Bypass Vulnerability","bugIDs":["CSCvw53542"],"ipsSignatures":["NA"],"cves":["CVE-2021-34705"],"cvrfUrl":"https://tools.cisco.com/security/center/contentxml/CiscoSecurityAdvisory/cisco-sa-fxo-pattern-bypass-jUXgygYv/cvrf/cisco-sa-fxo-pattern-bypass-jUXgygYv_cvrf.xml","cvssBaseScore":"5.3","cwe":["CWE-232"],"iosRelease":["15.1(4)M4"],"firstFixed":["15.7(3)M8"],"firstPublished":"2021-09-22T16:00:00","lastUpdated":"2021-09-22T16:00:00","status":"Final","version":"1.0","productNames":["Cisco IOS ","Cisco IOS XE Software ","Cisco IOS 12.3(9a)","Cisco IOS 12.3(15)",(...),"Cisco IOS XE Software 17.3.1w","Cisco IOS XE Software 17.3.2a","Cisco IOS XE Software 17.3.1x","Cisco IOS XE Software 17.3.1z","Cisco IOS XE Software 17.4.1","Cisco IOS XE Software 17.4.1a","Cisco IOS XE Software 17.4.1b","Cisco IOS XE Software 17.4.1c","Cisco IOS XE Software 17.6.1w"],"publicationUrl":"https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-fxo-pattern-bypass-jUXgygYv","sir":"High","summary":"\n<p>A vulnerability in the Voice Telephony Service Provider (VTSP) service of Cisco&nbsp;IOS Software and Cisco&nbsp;IOS XE Software could allow an unauthenticated, remote attacker to bypass configured destination patterns and dial arbitrary numbers.</p>\n<p>This vulnerability is due to insufficient validation of dial strings at Foreign Exchange Office (FXO) interfaces. An attacker could exploit this vulnerability by sending a malformed dial string to an affected device via either the ISDN protocol or SIP. A successful exploit could allow the attacker to conduct toll fraud, resulting in unexpected financial impact to affected customers.</p>\n<p>Cisco&nbsp;has released software updates that address this vulnerability. There are no workarounds that address this vulnerability.</p>\n<p>This advisory is available at the following link:<br><a href=\"https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-fxo-pattern-bypass-jUXgygYv\" target=\"_blank\">https://tools.cisco.com/security/center/content/CiscoSecurityAdvisory/cisco-sa-fxo-pattern-bypass-jUXgygYv</a></p>\n\n<p>This advisory is part of the September 2021 release of the Cisco&nbsp;IOS and IOS XE Software Security Advisory Bundled Publication. For a complete list of the advisories and links to them, see <a href=\"https://tools.cisco.com/security/center/viewErp.x?alertId=ERP-74581\">Cisco&nbsp;Event Response: September 2021 Semiannual Cisco&nbsp;IOS and IOS XE Software Security Advisory Bundled Publication.</a></p>\n\n"},{"advisoryId":"cisco-sa-info-disclosure-V4BmJBNF",etc (it repeats)

任何帮助都是非常感谢的。谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-02-18 23:55:14

您似乎希望在Python中使用这些建议,或者重新格式化并打印出来。

需要理解的最重要的事情是,json.load将为您完成这里的所有工作,因此您不必使用rereadlines

下面是一个例子:

代码语言:javascript
复制
with open('test.txt', errors='ignore') as file:
    data = json.load(file)

for advisory in data['advisories']:
    print('advisoryId = ' + repr(advisory['advisoryId']))
票数 1
EN

Stack Overflow用户

发布于 2022-02-19 00:51:46

首先,您需要验证json语法。

http://json.parser.online.fr/

  • paste
  1. go
  2. text.txt文件,并看到没有错误

然后

代码语言:javascript
复制
with open('text.txt','r', encoding='utf8', errors='ignore') as file:
    data = json.load(file)

for advisories in data['advisories']:
    keyList =  advisories.keys()
    for key in keyList:
        print(key,'::', advisories[key])

结果是

advisoryId :思科-sa-fxo-模式-旁路-jUXgygYv

advisoryTitle :思科IOS和IOS XE软件FXO接口目的地

bugIDs ::CSCvw53542

ipsSignatures ::'NA‘

.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71181028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档