[Python] Python script exports only half the information from the JSON

Discussão em 'Python' iniciado por Stack, Janeiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Python script but when it is exporting the JSON, it prints only "y", "meas", and "label" without any data. I have no errors when I run the script. Can you advise to fix that? I cant post the link and the JSON.

    import requests
    from pandas.io.json import json_normalize
    import json
    import pandas as pd
    import numpy as np

    # try this

    total_df = pd.DataFrame()
    request_list = ["LInk",]

    # read each link
    for r in request_list:
    resp = requests.get(url=r)
    df = json_normalize(resp.json())

    item_list = df['dataset']
    current_df = pd.DataFrame()



    for i in item_list:
    try:
    current_df.loc[0,'y'] = i['data'][0]['y']
    except:
    current_df.loc[0,'y'] = np.nan
    try:
    current_df.loc[0,'meas'] = i['meas']
    except:
    current_df.loc[0,'meas'] = np.nan
    try:
    current_df.loc[0,'label'] = i['label']
    except:
    current_df.loc[0,'label'] = np.nan

    total_df = pd.concat([total_df,current_df])


    total_df.to_excel('C:/Users/svetl/Onedrive/Desktop/work/output.xlsx',index=False)

    Continue reading...

Compartilhe esta Página