使用python从json文件检索数据

2024-10-02 02:44:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手。我正在Azure数据块上运行python。我有一个.json文件。我把json文件的重要字段放在这里

{
"school": [
    {
        "schoolid": "mr1",
        "board": "cbse",
        "principal": "akseal",
        "schoolName": "dps",
        "schoolCategory": "UNKNOWN",
        "schoolType": "UNKNOWN",
        "city": "mumbai",
        "sixhour": true,
        "weighting": 3,
        "paymentMethods": [
            "cash",
            "cheque"
        ],
        "contactDetails": [
            {
                "name": "picsa",
                "type": "studentactivities",
                "information": [
                    {
                        "type": "PHONE",
                        "detail": "+917597980"
                    }
                ]
            }
        ],
        "addressLocations": [
            {
                "locationType": "School",
                "address": {
                    "countryCode": "IN",
                    "city": "Mumbai",
                    "zipCode": "400061",
                    "street": "Madh",
                    "buildingNumber": "80"
                },
                "Location": {
                    "latitude": 49.313885,
                    "longitude": 72.877426
                },

我需要创建一个数据框,其中schoolName作为一列&;纬度及;经度是另外两列。你能告诉我怎么做吗


Tags: 文件数据boardjsonprincipalcitytypeazure
2条回答

用这个

import json # built-in
with open("filename.json", 'r') as jsonFile:
    Data = jsonFile.load()

数据现在是exp目录的字典

for i in Data:
    # loops through keys
    print(Data[i]) # prints the value

有关JSON的更多信息:

https://docs.python.org/3/library/json.html

和python字典:

https://www.programiz.com/python-programming/dictionary#:~:text=Python%20dictionary%20is%20an%20unordered,when%20the%20key%20is%20known

您可以使用方法json.load(),下面是一个示例:

import json

with open('path_to_file/file.json') as f:
    data = json.load(f)

print(data)

相关问题 更多 >

    热门问题