Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python导入JSON文件_Python_Json_Api - Fatal编程技术网

使用python导入JSON文件

使用python导入JSON文件,python,json,api,Python,Json,Api,我已经用python导入了一个json文件,并且仍然读取了第一个json元素 我需要添加一个循环来读取所有文件 JSON文件内容 [ { "severity": 4, "status": "OPEN", "id": 1987, "description": "Multiple Login Failures for the Same User containing Bad Userna

我已经用python导入了一个json文件,并且仍然读取了第一个json元素 我需要添加一个循环来读取所有文件

JSON文件内容

 [
        {
            "severity": 4,
            "status": "OPEN",
            "id": 1987,
            "description": "Multiple Login Failures for the Same User containing Bad Username",
            "start_time": 1525269490400
        },
        {
            "severity": 4,
            "status": "OPEN",
            "id": 1986,
            "description": "Multiple Login Failures for the Same User containing Bad Username",
            "start_time": 1525269181679
        },
.
.
.
.
.
    ]
这是python脚本

# Prepare the sample Alert

with open('output.json') as json_data:
    data = json.load(json_data,) 
if severity=data[0]['severity'] < 4:
    severity=1
elif severity=data[0]['severity'] > 6:
    severity=3
else:
    severity=2  
alert = Alert(title=data[0]['description'],
              date=data[0]['start_time'],
              severity=severity,
              description='N/A',
              type='Offense',
              source='QradarSiem',
              sourceRef=data[0]['id'])
但是我不知道在哪里,怎么用,你能帮我吗

我知道我需要对f:data.append(json.loads(line))中的行使用

你到底为什么要这样做???你已经解析了整个文件,你有了一个列表对象,你所要做的就是在列表上迭代

with open('output.json') as json_data:
    data = json.load(json_data,) 

for item in data:
    print(item)
我知道我需要对f:data.append(json.loads(line))中的行使用

你到底为什么要那样做???你已经解析了整个文件,你有了一个列表对象,你所要做的就是在列表上迭代

with open('output.json') as json_data:
    data = json.load(json_data,) 

for item in data:
    print(item)
排队

 data = json.load(json_data,)
您已经加载了所有数据。现在您可以在“数据”上迭代:

for item in data:
    if item['severity'] == 4:
        do_something(item)
排队

 data = json.load(json_data,)
您已经加载了所有数据。现在您可以在“数据”上迭代:

for item in data:
    if item['severity'] == 4:
        do_something(item)
尝试:

尝试:


如果severity=data[0]['severity']<4
是无效的Python。在英语中,“boucle”拼写为“loop”。请重新措辞。你的问题不清楚。你想干什么?什么是“boucle”?
如果severity=data[0]['severity']<4
是无效的Python。在英语中,“boucle”拼写为“loop”。请重新措辞。你的问题不清楚。你想干什么?什么是“团块”?你能解释一下它的作用吗?仅仅提供代码并不足以提供完整的上下文来支持您的答案。您能解释一下这是怎么回事吗?仅仅提供代码并不足以提供完整的上下文来支持您的答案。