如何解析卷曲响应而不将其保存到文件中

问题描述 投票:1回答:2

我正在尝试从GitLab中的文件中获取信息,然后解析该文件中的每个元素。

我能够将其保存到文件中,并且可以毫无问题地解析该文件。现在,我想使用curl获取此文件的信息并解析它,而无需先将其保存到文本文件中。

Python代码:例如,在这里我要查看AVP的版本,这是我所做的:

import subprocess
cmd = "curl -request GET --header 'PRIVATE-TOKEN'..............

x = subprocess.check_output(cmd, shell=True)
print(x.scripts)// error
print(x[1])// r
//I just want to get the script version which is 0.41

文件结构是这样的:

{
"AVP": "0.8",
"scripts": "0.41",
"systemlevel": "0.05",
"command": [],
"targeted": {}
}

我的问题是我正在获取数据,但是例如当我执行resp.scripts时,它将引发错误。

python curl subprocess
2个回答
0
投票

在您的情况下,x是字符串,您无法访问不存在的属性scripts。使用x[1],您将获得字符串的第二个字符。

两个可能的解决方案:

  • 将呼叫原样保持为curl并解析响应字符串:subprocess.check_output
  • 或者(最好,我认为)对您的HTTP请求使用类似How to parse JSON in Python?的Python模块:requests

0
投票

[在Python中避免使用HTTP requests and JSON parsing in Python。使用curl

requests
© www.soinside.com 2019 - 2024. All rights reserved.