0

我正在尝试在我的 streamlit 应用程序中加载一个 json 文件并将其解析为 pandas 数据帧。这是json文件:

{
"school_name": "local primary school",
"class": "Year 1",
"info": {
  "president": "John Kasich",
  "address": "ABC road, London, UK",
  "contacts": {
    "email": "admin@e.com",
    "tel": "123456789"
  }
},
"students": [
{
    "id": "A001",
    "name": "Tom",
    "math": 60,
    "physics": 66,
    "chemistry": 61
},
{
    "id": "A002",
    "name": "James",
    "math": 89,
    "physics": 76,
    "chemistry": 51
},
{
    "id": "A003",
    "name": "Jenny",
    "math": 79,
    "physics": 90,
    "chemistry": 78
}]
}

我正在使用 json_normalize 但我收到以下错误“TypeError:字符串索引必须是整数”

def show_students(file):
    json=pd.read_json(file)
    df=pd.json_normalize(
        json,
        record_path =['students'], 
        meta=[
            'class',
            ['info', 'president'], 
            ['info', 'contacts', 'tel']
        ]
    )
    st.write(df)

uploaded_file = st.file_uploader("Choose a file", type=['json'])
    if uploaded_file is not None:
    show_students(uploaded_file)

知道我该如何解决这个问题吗?谢谢!

4

0 回答 0