简体   繁体   English

如何解决TypeError:列表索引必须是整数或切片,而不是python中的str

[英]how to solve TypeError: list indices must be integers or slices, not str in python

The Situation情况

I am trying to access values within a json string but receive the error:我试图访问 json 字符串中的值但收到错误:

TypeError: list indices must be integers or slices, not str类型错误:列表索引必须是整数或切片,而不是 str

The JSON JSON

        j={
        "type": "FeatureCollection",
        "features": [
            {
                "type": "Feature",
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        2.44775,
                        48.925778
                    ]
                },
                "properties": {
                    "country_code": "fr",
                    "housenumber": "15",
                    "street": "Rue Leon Bernard",
                    "distance": 16.948223707815888,
                    "country": "France",
                    "county": "Seine-Saint-Denis",
                    "datasource": {
                        "sourcename": "openaddresses",
                        "attribution": "© OpenAddresses contributors",
                        "license": "BSD-3-Clause License"
                    },
                    "postcode": "93700",
                    "state": "Ile-of-France",
                    "district": "Drancy",
                    "city": "Drancy",
                    "lon": 2.44775,
                    "lat": 48.925778,
                    "result_type": "building",
                    "formatted": "15 Rue Leon Bernard, 93700 Drancy, France",
                    "address_line1": "15 Rue Leon Bernard",
                    "address_line2": "93700 Drancy, France",
                }
            }
        ]
    }

The Objective目标

I need to get the value of country_code and country i have tried我需要获取country_code 和country 的值我已经尝试过

j['features']['properties'][0]` 

The Question:问题:

How can i get the value of these two fields in python ?如何在python中获取这两个字段的值?

In the json that you provided the 'features' value is a list, thus you need to index it accordingly.在您提供的 json 中,'features' 值是一个列表,因此您需要相应地对其进行索引。

j['features'][0]['properties']

From here you should be able to simply continue to access all the values of 'properties'从这里您应该能够简单地继续访问“属性”的所有值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决“类型错误:列表索引必须是整数或切片,而不是 str” - How to solve "TypeError: list indices must be integers or slices, not str" Python3-TypeError:列表索引必须是整数或切片,而不是str-List - Python3 - TypeError: list indices must be integers or slices, not str - List “TypeError:list indices必须是整数或切片,而不是str” - “TypeError: list indices must be integers or slices, not str” TypeError:列表索引必须是整数或切片,而不是 str - TypeError: List indices must be integers or slices and not str 类型错误:列表索引必须是整数或切片,而不是 str - TypeError: list indices must be integers or slices, not str “TypeError:列表索引必须是整数或切片,而不是 str” - "TypeError: list indices must be integers or slices, not str" TypeError:列表索引必须是整数或切片而不是 str - TypeError: list indices must be integers or slices not str 类型错误:列表索引必须是整数或切片,而不是 str - TypeError: list indices must be integers or slices, not str 如何用字典列表解决“TypeError: list indices must be integers or slice, not str”? - How to solve "TypeError: list indices must be integers or slices, not str" with a list of dictionaries? 使用 Python TypeError 解析 JSON:列表索引必须是整数或切片,而不是 str - Parsing JSON with Python TypeError: list indices must be integers or slices, not str
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM