【发布时间】:2023-03-24 02:42:01
【问题描述】:
我想遍历这本字典并找到任何具有前导零的“id”,如下面的,然后将其替换为不带零的。所以 'id': '01001' 会变成 'id': '1001'
以下是获取我正在使用的数据的方法:
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
counties = json.load(response)
到目前为止,我已经能够一次获取一个 ID,但不确定如何循环获取所有 ID:
到目前为止我的代码:counties['features'][0]['id']
{ 'type': 'FeatureCollection',
'features': [{'type': 'Feature',
'properties': {'GEO_ID': '0500000US01001',
'STATE': '01',
'COUNTY': '001',
'NAME': 'Autauga',
'LSAD': 'County',
'CENSUSAREA': 594.436},
'geometry': {'type': 'Polygon',
'coordinates': [[[-86.496774, 32.344437],
[-86.717897, 32.402814],
[-86.814912, 32.340803],
[-86.890581, 32.502974],
[-86.917595, 32.664169],
[-86.71339, 32.661732],
[-86.714219, 32.705694],
[-86.413116, 32.707386],
[-86.411172, 32.409937],
[-86.496774, 32.344437]]]},
'id': '01001'}
]
}
【问题讨论】:
-
你已经用熊猫标记了它,这是熊猫问题吗?如果是这样,请包括数据框的示例。
-
请附上您迄今为止尝试解决问题的代码。
-
好的,我添加了如何获取我正在使用的数据
-
您需要遍历字典并使用lstrip 去除前导零的
for x in counties['features']: x['id'] = x['id'].lstrip('0')