Python 迭代字典以替换前导零?

Python 迭代字典以替换前导零?,python,hyphen,Python,Hyphen,我想遍历这个字典,找到任何有前导零的'id',比如下面的一个,并替换为没有前导零的'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

我想遍历这个字典,找到任何有前导零的'id',比如下面的一个,并替换为没有前导零的'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:

到目前为止我的代码:
countries['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'}
    ]
}
然后,如果id与JSON结构相同,则遍历该列表。并更新id 作为

countries['features'][0]['id']=countries['features'][0]['id'].lstrip(“0”)

lstrip将从字符串中删除前导零。

假设您的字典包含以下数据。您可以使用以下代码:

counties={'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'}, {'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': '000000000001001'} ]} 

for feature in counties['features']:
    feature ['id']=feature ['id'].lstrip("0")

print(counties)    

下面是使用json对象挂钩实现这一点的更短更快的方法

def stripZeroes(d):
    if 'id' in d:
        d['id'] = d['id'].lstrip('0')
        return d
    return d
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response, object_hook=stripZeroes)

你给它贴上了熊猫的标签,这是熊猫的问题吗?如果是这样,请包括数据帧的示例。请包括您迄今为止试图解决该问题的代码。好的,我添加了如何获取我正在处理的数据。您将需要在dict上循环并使用该dict来剥离县['features']中x的前导零
:x['id']=x['id']。lstrip('0')
def stripZeroes(d):
    if 'id' in d:
        d['id'] = d['id'].lstrip('0')
        return d
    return d
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response, object_hook=stripZeroes)