Python-从嵌套json提取数据时出现问题

马里奥

我从json提取数据时遇到问题,我尝试了n种不同的方法。我能够提取ID本身,很遗憾,我无法显示该字段的详细信息。
以下是我的json

{
    "params": {
        "cid": "15482782896",
        "datemax": "20190831",
        "datemin": "20190601",
        "domains": [
            "url.com"
        ],

    },
    "results": {
        "59107": {
            "url.com": {
                "1946592": {
                    "data": {
                        "2019-06-01": {
                            "ENGINE": {
                                "DEVICE": {
                                    "": {
                                        "position": 21,
                                        "url": "url3.com"
                                    }
                                }
                            }
                        },
                        "2019-07-01": {
                            "ENGINE": {
                                "DEVICE": {
                                    "": {
                                        "position": 4,
                                        "url": "url3.com"
                                    }
                                }
                            }
                        },
                        "2019-08-01": {
                            "ENGINE": {
                                "DEVICE": {
                                    "": {
                                        "position": 2,
                                        "url": "url3.com"
                                    }
                                }
                            }
                        }
                    },
                    "keyword": {
                        "title": "python_1",
                        "volume": 10
                    }
                },
                "1946602": {
                    "data": {
                        "2019-06-01": {
                            "ENGINE": {
                                "DEVICE": {
                                    "": {
                                        "position": 5,
                                        "url": "url1.com"
                                    }
                                }
                            }
                        },
                        "2019-07-01": {
                            "ENGINE": {
                                "DEVICE": {
                                    "": {
                                        "position": 12,
                                        "url": "url1.com"
                                    }
                                }
                            }
                        },
                        "2019-08-01": {
                            "ENGINE": {
                                "DEVICE": {
                                    "": {
                                        "position": 10.25,
                                        "url": "url1.com"
                                    }
                                }
                            }
                        }
                    },
                    "keyword": {
                        "title": "python_2",
                        "volume": 20
                    }
                }
            }
        }
    }
}

我尝试了以下代码,但以id本身的形式得到了结果

import json
import csv

def get_leaves(item, key=None):
    if isinstance(item, dict):
        leaves = {}
        for i in item.keys():
            leaves.update(get_leaves(item[i], i))
        return leaves
    elif isinstance(item, list):
        leaves = {}
        for i in item:
            leaves.update(get_leaves(i, key))
        return leaves
    else:
        return {key : item}


with open('me_filename') as f_input:
    json_data = json.load(f_input)

fieldnames = set()

for entry in json_data:
    fieldnames.update(get_leaves(entry).keys())

with open('output.csv', 'w', newline='') as f_output:
    csv_output = csv.DictWriter(f_output, fieldnames=sorted(fieldnames))
    csv_output.writeheader()
    csv_output.writerows(get_leaves(entry) for entry in json_data)

我也尝试使用熊猫但也无法正确解析

import io
import json
import pandas as pd

with open('me_filename', encoding='utf-8') as f_input:
    df = pd.read_json(f_input , orient='None')

df.to_csv('output.csv', encoding='utf-8')

结果我需要得到它:

ID      Name         page   volume  url      2019-06-01 2019-07-01  2019-08-01  2019-09-01
1946592 python_1    url.com 10      url3.com    21   4    2 null
1946602 python_2    url.com 20      url1.com    5   12  10,25   null

我该怎么办?

约书亚装载机

嗯,这是一个复杂的解决方案,看起来非常混乱,不再像提供的代码一样,但是我相信它将解决您的问题。

首先,我对提供的Json遇到了问题(由于在第8行的末尾加上了','),但是设法生成了:

输出(temp.csv)

ID,Name,Page,Volume,Url,2019-08-01,2019-07-01,2019-06-01,
1946592,python_1,url.com,10,url3.com,2,4,21,
1946602,python_2,url.com,20,url1.com,10.25,12,5,

使用以下内容:

import json

dates: set = set()

# Collect the data 
def get_breakdown(json):
    collected_data = []
    for result in json['results']:
        for page in json['results'][result]:
            for _id in json['results'][result][page]:
                data_struct = { 
                    'ID': _id, 
                    'Name': json['results'][result][page][_id]['keyword']['title'], 
                    'Page': page, 
                    'Volume': json['results'][result][page][_id]['keyword']['volume'], 
                    'Dates': {}
                }
                for date in dates:
                    if date in json['results'][result][page][_id]['data']:
                        data_struct['URL'] = json['results'][result][page][_id]['data'][date]['ENGINE']['DEVICE']['']['url']
                        data_struct['Dates'][date] = {'Position' : json['results'][result][page][_id]['data'][date]['ENGINE']['DEVICE']['']['position']}
                    else:
                        data_struct['Dates'][date] = {'Position' : 'null'}
                collected_data.append(data_struct)
    return collected_data

# Collect all dates across the whole data 
# structure and save them to a set
def get_dates(json):
    for result in json['results']:
        for page in json['results'][result]:
            for _id in json['results'][result][page]:
                for date in json['results'][result][page][_id]['data']:
                    dates.add(date)


# Write to .csv file
def write_csv(collected_data, file_path):
    f = open(file_path, "w")
    # CSV Title
    date_string = ''
    for date in dates:
        date_string = '{0}{1},'.format(date_string, date)
    f.write('ID,Name,Page,Volume,Url,{0}\n'.format(date_string))

    # Data
    for data in collected_data:
        position_string = ''
        for date in dates:
            position_string = '{0}{1},'.format(position_string, data['Dates'][date]['Position'])
        f.write('{0},{1},{2},{3},{4},{5}\n'.format(
            data['ID'],
            data['Name'],
            data['Page'],
            data['Volume'],
            data['URL'],
            position_string
        ))

# Code Body
with open('me_filename.json') as f_input:
    json_data = json.load(f_input)
    get_dates(json_data)
    write_csv(get_breakdown(json_data), "output.csv")

希望您可以遵循该代码,并且可以完成预期的工作。我确信它可以变得更加可靠-但是,如前所述,我无法使其与您提供的基本代码一起使用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Python脚本中解析JSON数据时出现问题

从python脚本打印json数据时出现问题

用python发布json数据时出现问题

Python:将数据写入bootstrapTable时出现问题

在 Python 列表中存储 API 数据时出现问题

Python + Pandas:读取json时出现问题-奇怪的结构

Python:从列表的迭代中提取特定项目时出现问题,(“字符串”,)

Python:读写文件时出现问题

下载python 3时出现问题

安装python时出现问题

在Python中将列表转换为嵌套字典时出现问题

使用嵌套的for循环比较Python中的2个Excel工作表时出现问题

显示来自嵌套JSON结构的数据时出现问题

从json检索嵌套数据时出现问题

在Python中通过后期请求发送数据时出现问题

尝试读取使用Python数组模块创建的数据文件时出现问题

在将数据帧写入/读取到CSV时出现问题-最终形状-Python

当我尝试在数据矩阵上添加一层时出现问题(python)

将python数据框汇总到每条记录一行时出现问题

使用Pandas Python(pandas.io.parsers.TextFileReader)从文件读取数据时出现问题

Python数据框:尝试按多列分组时出现问题

在数据框中使用“ For”和“ If”时出现问题(Python)

使用Python将SQL输出读取到SAS数据集中时出现问题

使用tkinter,python将输入数据显示到文本框中时出现问题

从json提取text []时出现问题

捕获 json 值并将这些值分配给 python 列表时出现问题

使用ftp通过python上传JSON文件时出现问题

将JSON信息存储到字典中以在python中循环时出现问题

在python中解析Alexa json响应以获取值名称时出现问题