我正在尝试从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
时,它将引发错误。
在您的情况下,x
是字符串,您无法访问不存在的属性scripts
。使用x[1]
,您将获得字符串的第二个字符。
两个可能的解决方案:
curl
并解析响应字符串:subprocess.check_output
requests
[在Python中避免使用HTTP requests and JSON parsing in Python。使用curl
requests