csv Json数据到原始数据

vwhgwdsa  于 2023-03-15  发布在  其他
关注(0)|答案(2)|浏览(164)

我需要你们的帮助,我需要把这个JSON数据转换成原始数据,但是我不想在这个json上面使用很多for来迭代,所以你们知道如何用一种更好的方式来做吗?
但我不知道如何以“简单”的方式做到这一点,我不想for: for: for: for
谢谢你们!

编辑

包含全部数据的JSON输入:

[
    {
        "name": "Dummy_App_Name",
        "appKey": "Dummy_App_Key",
        "platform": "Dummy_Platform",
        "data": [
            [
                {
                    "id": "ffb1e945-f619-48d9-ab7f-e7a2c1792003",
                    "name": "Dummy_Ad_Network_Instance_1",
                    "contents": [
                        {
                            "id": "Dummy_id",
                            "name": "Dummy_Name",
                            "isSkippable": True,
                            "offerwallAbTest": None,
                            "type": "Dummy_Type",
                            "insights": {
                                "reports": [
                                    {
                                        "country": "TD",
                                        "clicks": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "conversions": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "impressions": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "dailyUniqueViewers": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "dailyUniqueConversions": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "earnings": [ 0, 1, 2, 3, 4, 5, 6 ],
                                    },
                                    {
                                        "country": "SC",
                                        "clicks": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "conversions": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "impressions": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "dailyUniqueViewers": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "dailyUniqueConversions": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "earnings": [ 0, 1, 2, 3, 4, 5, 6 ],
                                    }
                                ],
                                "timestamps": [
                                    "2023-03-06T00:00:00Z",
                                    "2023-03-07T00:00:00Z",
                                    "2023-03-08T00:00:00Z",
                                    "2023-03-09T00:00:00Z",
                                    "2023-03-10T00:00:00Z",
                                    "2023-03-11T00:00:00Z",
                                    "2023-03-12T00:00:00Z"
                                ]
                            }
                        }
                    ]
                },
                {
                    "id": "be70f064-6226-412f-942c-2a2eeabb8d79",
                    "name": "Dummy_Ad_Network_Instance_2",
                    "contents": [
                        {
                            "id": "Dummy_Id",
                            "name": "Dummy_Name",
                            "isSkippable": True,
                            "offerwallAbTest": None,
                            "type": "Dummy_Type",
                            "insights": {
                                "reports": [
                                    {
                                        "country": "BY",
                                        "clicks": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "conversions": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "impressions": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "dailyUniqueViewers": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "dailyUniqueConversions": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "earnings": [ 0, 1, 2, 3, 4, 5, 6 ],
                                    },
                                    {
                                        "country": "CA",
                                        "clicks": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "conversions": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "impressions": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "dailyUniqueViewers": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "dailyUniqueConversions": [ 0, 1, 2, 3, 4, 5, 6 ],
                                        "earnings": [ 0, 1, 2, 3, 4, 5, 6 ]
                                    }
                                ],
                                "timestamps": [
                                    "2023-03-06T00:00:00Z",
                                    "2023-03-07T00:00:00Z",
                                    "2023-03-08T00:00:00Z",
                                    "2023-03-09T00:00:00Z",
                                    "2023-03-10T00:00:00Z",
                                    "2023-03-11T00:00:00Z",
                                    "2023-03-12T00:00:00Z"
                                ]
                            }
                        }
                    ]
                }
            ]
        ]
    }
]

预期产出

"date", "app_name", "appKey", "platform", "ad_network_instance", "placement", "country", "earnings", "impressions", "clicks", "conversions", "ecpm", "dailyUniqueViewers", "dailyUniqueConversions"
"2023-03-06T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","TD",0,0,0,0,((earning/1000000)/impressions)*1000,0,0,0
"2023-03-07T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","TD",1,1,1,1,((earning/1000000)/impressions)*1000,1,1,1
"2023-03-08T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","TD",2,2,2,2,((earning/1000000)/impressions)*1000,2,2,2
"2023-03-09T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","TD",3,3,3,3,((earning/1000000)/impressions)*1000,3,3,3
"2023-03-10T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","TD",4,4,4,4,((earning/1000000)/impressions)*1000,4,4,4
"2023-03-11T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","TD",5,5,5,5,((earning/1000000)/impressions)*1000,5,5,5
"2023-03-12T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","TD",6,6,6,6,((earning/1000000)/impressions)*1000,6,6,6
"2023-03-06T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","SC",0,0,0,0,((earning/1000000)/impressions)*1000,0,0,0
"2023-03-07T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","SC",1,1,1,1,((earning/1000000)/impressions)*1000,1,1,1
"2023-03-08T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","SC",2,2,2,2,((earning/1000000)/impressions)*1000,2,2,2
"2023-03-09T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","SC",3,3,3,3,((earning/1000000)/impressions)*1000,3,3,3
"2023-03-10T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","SC",4,4,4,4,((earning/1000000)/impressions)*1000,4,4,4
"2023-03-11T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","SC",5,5,5,5,((earning/1000000)/impressions)*1000,5,5,5
"2023-03-12T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_1","SC",6,6,6,6,((earning/1000000)/impressions)*1000,6,6,6
"2023-03-06T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","BY",0,0,0,0,((earning/1000000)/impressions)*1000,0,0,0
"2023-03-07T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","BY",1,1,1,1,((earning/1000000)/impressions)*1000,1,1,1
"2023-03-08T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","BY",2,2,2,2,((earning/1000000)/impressions)*1000,2,2,2
"2023-03-09T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","BY",3,3,3,3,((earning/1000000)/impressions)*1000,3,3,3
"2023-03-10T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","BY",4,4,4,4,((earning/1000000)/impressions)*1000,4,4,4
"2023-03-11T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","BY",5,5,5,5,((earning/1000000)/impressions)*1000,5,5,5
"2023-03-12T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","BY",6,6,6,6,((earning/1000000)/impressions)*1000,6,6,6
"2023-03-06T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","CA",0,0,0,0,((earning/1000000)/impressions)*1000,0,0,0
"2023-03-07T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","CA",1,1,1,1,((earning/1000000)/impressions)*1000,1,1,1
"2023-03-08T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","CA",2,2,2,2,((earning/1000000)/impressions)*1000,2,2,2
"2023-03-09T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","CA",3,3,3,3,((earning/1000000)/impressions)*1000,3,3,3
"2023-03-10T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","CA",4,4,4,4,((earning/1000000)/impressions)*1000,4,4,4
"2023-03-11T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","CA",5,5,5,5,((earning/1000000)/impressions)*1000,5,5,5
"2023-03-12T00:00:00Z","Dummy_App_Name","Dummy_App_Key","Dummy_Platform","Dummy_Ad_Network_Instance_2","CA",6,6,6,6,((earning/1000000)/impressions)*1000,6,6,6
ruarlubt

ruarlubt1#

即使它们看起来像数据行,您也可以将时间戳和其他见解/报告视为 * 列 *:
1.取每个列表并附加到列列表中。
1.使用zip(*cols)函数“旋转”或“转置”列列表以获得行列表。
首先,预先定义标题:

headers = ["timestamps", "clicks", "conversions", "impressions", "dailyUniqueViewers", "dailyUniqueConversions", "earnings"]

创建列列表并使用时间戳列表对其进行初始化:

cols = [data["insights"]["timestamps"]]

循环标题(跳过时间戳headers[1:])并将每个报告数据列表附加到列:

for header in headers[1:]:
    cols.append(data["insights"]["reports"][0][header])

按原样打印,我们会看到:

[
    ["2023-03-06T00:00:00Z", "2023-03-07T00:00:00Z", "2023-03-08T00:00:00Z", "2023-03-09T00:00:00Z", "2023-03-10T00:00:00Z", "2023-03-11T00:00:00Z", "2023-03-12T00:00:00Z"],
    [0, 1, 2, 3, 4, 5, 6],
    [0, 1, 2, 3, 4, 5, 6],
    [0, 1, 2, 3, 4, 5, 6],
    [0, 1, 2, 3, 4, 5, 6],
    [0, 1, 2, 3, 4, 5, 6],
    [0, 1, 2, 3, 4, 5, 6],
]

打印标题,然后将每列压缩到列中(记住星星*cols语法,这样zip实际上可以看到(cols[0], cols[1], ...)):

print(headers)
for row in zip(*cols):
    print(row)

你会看到正确的结构

['clicks', 'conversions', 'impressions', 'dailyUniqueViewers', 'dailyUniqueConversions', 'earnings']
('2023-03-06T00:00:00Z', 0, 0, 0, 0, 0, 0)
('2023-03-07T00:00:00Z', 1, 1, 1, 1, 1, 1)
('2023-03-08T00:00:00Z', 2, 2, 2, 2, 2, 2)
('2023-03-09T00:00:00Z', 3, 3, 3, 3, 3, 3)
('2023-03-10T00:00:00Z', 4, 4, 4, 4, 4, 4)
('2023-03-11T00:00:00Z', 5, 5, 5, 5, 5, 5)
('2023-03-12T00:00:00Z', 6, 6, 6, 6, 6, 6)
  • (我不担心头是列表,行是元组,csv模块只需要一个序列来编码为字符串)*

从这里开始,引入csv模块,并使用它的writerow(headers)来编码头文件的单个列表,使用writerows(zip(*cols))来迭代zip操作的每个结果(行)(上面的正确结构):

import csv

with open("output.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerow(headers)
    writer.writerows(zip(*cols))

| 时间戳|喀嗒声|转换|印象|每日唯一查看器|每日唯一转换|收益|
| - ------|- ------|- ------|- ------|- ------|- ------|- ------|
| 2023年3月6日上午00时00分00秒|无|无|无|无|无|无|
| 2023年3月7日上午00时00分00秒|1个|1个|1个|1个|1个|1个|
| 2023年3月8日星期一00时00分00秒|第二章|第二章|第二章|第二章|第二章|第二章|
| 2023年3月9日上午00时00分00秒|三个|三个|三个|三个|三个|三个|
| 2023年3月10日上午00时00分00秒|四个|四个|四个|四个|四个|四个|
| 2023年3月11日上午00时00分00秒|五个|五个|五个|五个|五个|五个|
| 2023年3月12日零时00分00秒|六个|六个|六个|六个|六个|六个|

w8rqjzmb

w8rqjzmb2#

解决此问题的代码是:

def organize_graph_response(graph_results):
    res = []
    for graph_result in graph_results:
        for data in graph_result["data"][0]:
            if len(data["contents"]) > 0:
                if data["contents"][0]["insights"]["reports"][0]["earnings"]:
                    revenue = data["contents"][0]["insights"]["reports"][0]["earnings"]
                    impressions = data["contents"][0]["insights"]["reports"][0]["impressions"]
                    timestamps = data["contents"][0]["insights"]["timestamps"]
                    reports = data["contents"][0]["insights"]["reports"][0]
                    cols = [data["contents"][0]["insights"]["timestamps"]]  # DATE
                    cols.append([graph_result["app_name"]] * lookback_count(timestamps))  # APP_NAME
                    cols.append([graph_result["appKey"]] * lookback_count(timestamps))  # APPKEY
                    cols.append([graph_result["platform"]] * lookback_count(timestamps))  # APPKEY
                    cols.append([data["name"]] * lookback_count(timestamps))  # AD_NETWORK_INSTANCE
                    cols.append([data["contents"][0]["name"]] * lookback_count(timestamps))  # PLACEMENT
                    cols.append([reports["country"]] * lookback_count(timestamps))  # COUNTRY
                    cols.append(reports["clicks"])  # CLICKS
                    cols.append(reports["conversions"])  # CONVERSIONS
                    cols.append(reports["impressions"])  # IMPRESSIONS
                    cols.append(reports["dailyUniqueViewers"])  # DEU
                    cols.append(reports["dailyUniqueConversions"])  # DUC
                    cols.append(calculate_ecpm(revenue, impressions))  # ECPM
                    cols.append(reports["earnings"])  # REVENUE
                else:
                    revenue = 0
                    impressions = 0
            for row in zip(*cols):
                res.append(row)
    return res

相关问题