【问题标题】:Iterate through dictionary to replace leading zeros?遍历字典以替换前导零?
【发布时间】: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')

标签: python hyphen


【解决方案1】:
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 与您的 JSON 结构一致,则遍历列表。并更新 id 作为

counties['features'][0]['id'] = counties['features'][0]['id'].lstrip("0") lstrip 将从字符串中删除前导零。

【讨论】:

    【解决方案2】:

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

    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)    
    

    【讨论】:

      【解决方案3】:

      这里是使用 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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-05-29
        • 2019-09-24
        • 2016-12-05
        • 2019-06-06
        相关资源
        最近更新 更多