Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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中遍历列表的列表_Python_Json_List_Nested - Fatal编程技术网

在python中遍历列表的列表

在python中遍历列表的列表,python,json,list,nested,Python,Json,List,Nested,我正在从API中阅读以下json嵌套字典 { "result": [{ "short_description": "I am getting bluescreen error", "sys_id": "39b5f8c2376ede007520021a54990e5c", "opened_at": "2016-04-04 05:19:53", "number":"INC0258

我正在从API中阅读以下json嵌套字典

{
 "result": [{
               "short_description": "I am getting bluescreen error",
               "sys_id": "39b5f8c2376ede007520021a54990e5c",
               "opened_at": "2016-04-04 05:19:53",
               "number":"INC0258523"
             },
             {
               "short_description": "laptop crashed with a blue screen",
               "sys_id": "da0095380f43d200a4f941fce1050eeb",
               "opened_at":"2016-04-25 06:33:52",
               "number":"INC0259067"
             }, 
             {
               "short_description": "Laptop not booting",
               "sys_id": "ecf9c9b00f43d200a4f941fce1050e17",
               "opened_at": "2016-04-25 06:07:16",
               "number": "INC0259061"
             }]
}
我需要根据两个起始日期和两个日期筛选数据
opened\u at
是包含日期信息的值。

到目前为止,我的尝试如下

    url = "http://ip:port/api"

    response = urllib.urlopen(url)
    data = json.loads(response.read())
    print type(data)
    pattern = 'opened_at'
    element = '2016-04-25 06:33:19'
    with open('D:/Output.csv', 'wb') as f:  
        w = csv.DictWriter(f, data['result'][0].keys()) 
        w.writeheader()
        print type(data['result'])
        for key in data['result']:
            for v, val in data['result'].items():
                if v == pattern and val == element:
                    w.writerow(v)
我在运行代码时遇到以下错误

AttributeError: 'list' object has no attribute 'items'

我知道数据类型['result']是一个列表。如有任何帮助,将不胜感激。谢谢

data['result']
是一个字典列表,您应该按如下方式对其进行迭代:

for d in data['result']:
    for k, v in d.items():
你需要这个

import json
al = """
{
 "result": [{
               "short_description": "I am getting bluescreen error",
               "sys_id": "39b5f8c2376ede007520021a54990e5c",
               "opened_at": "2016-04-04 05:19:53",
               "number":"INC0258523"
             },
             {
               "short_description": "laptop crashed with a blue screen",
               "sys_id": "da0095380f43d200a4f941fce1050eeb",
               "opened_at":"2016-04-25 06:33:52",
               "number":"INC0259067"
             }, 
             {
               "short_description": "Laptop not booting",
               "sys_id": "ecf9c9b00f43d200a4f941fce1050e17",
               "opened_at": "2016-04-25 06:07:16",
               "number": "INC0259061"
             }]
}
"""
myjson = json.loads(al)
for val in myjson['result']:
    print val['opened_at']
    for key, value in val.items():#change it to keys if required
实际上,“结果”是一个词汇。您可以通过在Python中用“{”和“}”圈出的单词来识别它。
所以这里是一个词汇表的词汇表

这正是我在5分钟前发布的内容,只是重命名了OP变量…提供一些必需的输出,会更容易…输出应该是一个csv文件,内容仅在两个特定日期之间。假设我给出的是2016年4月4日,那么csv文件应该只包含2016年4月4日的记录。很抱歉,我没有从中获得所需的输出。输出文件中未写入任何内容。是否确定元素变量包含json中的值?你的例子并非如此。