簡體   English   中英

訪問字典元素的最Python方式

[英]The most Pythonic way to access elements of a dict

我多次調用API,最終將結果寫入CSV。 我有以下代碼從dict提取數據:

city = data['property'][0]['address']['locality']
zip_code = data['property'][0]['address']['postal1']
county = data['property'][0]['area']['countrysecsubd']
condition = data['property'][0]['building']['construction']['condition']
roof = data['property'][0]['building']['construction']['roofcover']
bathrooms = data['property'][0]['building']['rooms']['bathstotal']
bedrooms = data['property'][0]['building']['rooms']['beds']
total_number_of_rooms = data['property'][0]['building']['rooms']['roomsTotal']
square_footage = data['property'][0]['building']['size']['bldgsize']
year_built = data['property'][0]['summary']['yearbuilt']
number_of_stories = data['property'][0]['building']['summary']['levels']
lot_size1 = data['property'][0]['lot']['lotsize1']
lot_size2 = data['property'][0]['lot']['lotsize2']
latitude = data['property'][0]['location']['latitude']
longitude = data['property'][0]['location']['longitude']

該結構dict在這里上引擎收錄,因為它占用了大量的空間。

如何用更少的代碼獲得相同的結果?

編寫一個幫助程序函數,可以減少編寫樣板的次數:

def follow(obj, path):
     for seg in path.split():
          obj = obj[int(seg) if seg.isdigit() else seg]
     return obj

致電:

prop = follow(data, "property 0")
city = follow(prop, "address locality")

等等

這樣創建自己的字典類呢?

class DictQuery(dict):
    def get(self, path, default = None):
          keys = path.split("/")
          val = None

          for key in keys:
              if val:
                  if isinstance(val, list):
                      val = [ v.get(key, default) if v else None for v in val]
                  else:
                      val = val.get(key, default)
              else:
                  val = dict.get(self, key, default)

              if not val:
                  break;

          return val

然后,您可以這樣稱呼它

for row in csv:
    print(DictQuery(row).get("property/address"))

請注意,這未經測試,只是一個嘗試的想法。

您這樣做的方式是“ Pythonic”。 但是,還有其他選擇,一種是使用鍵,就像它們是對象(或子對象)屬性一樣。 仁者見仁,Python或多或少。 一旦安裝好腳手架,它肯定需要更少的打字,而且我覺得它也更容易閱讀。

實現類似方法的一種方法是創建一個自己的__dict__dict子類。 一旦定義了類,則必須將其中最外面的子詞典和所有子詞典轉換為AttrDict實例。

這是顯示我的意思的代碼:

import json

class AttrDict(dict):
    def __init__(self, *args, **kwargs):
        super(AttrDict, self).__init__(*args, **kwargs)
        self.__dict__ = self


def convert(d):
    """ Convert dict "d" and all nested dictionaries in it into AttrDicts. """
    def _decode_dict(a_dict):
        return AttrDict(a_dict)  # Turn each dictionary into an AttrDict

    json_repr = json.dumps(d)
    return json.loads(json_repr, object_hook=_decode_dict)

d = {u'status': {u'code': 0, u'pagesize': 10, u'version': u'1.0.0', u'msg': u'SuccessWithResult', u'total': 1, u'page': 1}, u'property': [{u'building': {u'summary': {u'bldgsNum': 1, u'unitsCount': u'0', u'archStyle': u'OLD', u'bldgType': u'SINGLE FAMILY', u'yearbuilteffective': 0, u'levels': 1, u'storyDesc': u'SINGLE FAMILY'}, u'construction': {u'roofcover': u'ASPHALT SHINGLE', u'wallType': u'WOOD SIDING', u'foundationtype': u'PIER', u'condition': u'FAIR'}, u'rooms': {u'bathstotal': 1.0, u'roomsTotal': 6, u'bathscalc': 1.0, u'bathfixtures': 0, u'bathsfull': 0, u'beds': 3, u'baths3qtr': 0, u'baths1qtr': 0, u'bathshalf': 0}, u'parking': {u'prkgType': u'DETACHED GARAGE', u'prkgSize': 0, u'garagetype': u'DETACHED GARAGE', u'prkgSpaces': u'0'}, u'interior': {u'fplctype': u'TYPE UNKNOWN', u'fplccount': 1, u'bsmtsize': 0, u'fplcind': u'Y'}, u'size': {u'universalsize': 1022, u'livingsize': 1022, u'sizeInd': u'LIVING SQFT ', u'grosssizeadjusted': 0, u'groundfloorsize': 1022, u'bldgsize': 1022, u'grosssize': 0}}, u'area': {u'taxcodearea': u'11', u'countrysecsubd': u'Bexar County', u'muncode': u'21', u'subdname': u'SOUTH PARK TERRACE BAITYS BL 3', u'countyuse1': u'A1', u'blockNum': u'11', u'munname': u'SAN ANTONIO'}, u'vintage': {u'lastModified': u'2017-9-23', u'pubDate': u'2017-10-12'}, u'utilities': {u'heatingtype': u'FORCED AIR', u'wallType': u'WOOD SIDING', u'coolingtype': u'AC.CENTRAL'}, u'summary': {u'proptype': u'SFR', u'propsubtype': u'SINGLE FAMILY', u'absenteeInd': u'SITUS FROM SALE (ABSENTEE)', u'propclass': u'Single Family Residence / Townhouse', u'yearbuilt': 1930, u'legal1': u'NCB 3130 BLK 11 LOT 35 AND 36', u'propIndicator': u'10', u'propLandUse': u'SFR'}, u'location': {u'distance': 0.0, u'elevation': 0.0, u'longitude': u'-98.484597', u'latitude': u'29.396664', u'geoid': u'CO48029,CS4893407,DB4838730,MT30003336,ND0000206694,ND0000567797,PL4865000,RS0000576252,SB0000123866,SB0000123853,SB0000123848,ZI78210', u'accuracy': u'Street'}, u'lot': {u'lotnum': u'35', u'depth': 133, u'lotsize2': 5320, u'frontage': 40, u'lotsize1': 0.1221}, u'address': {u'matchCode': u'ExaStr', u'postal2': u'3873', u'postal3': u'C002', u'locality': u'San Antonio', u'country': u'US', u'countrySubd': u'TX', u'line2': u'SAN ANTONIO, TX 78210', u'line1': u'202 LORETTA PL', u'postal1': u'78210', u'oneLine': u'202 LORETTA PL, SAN ANTONIO, TX 78210'}, u'identifier': {u'apn': u'031300110350', u'apnOrig': u'03130-011-0350', u'fips': u'48029', u'obPropId': 12253857148029}}]}

data = convert(d)
city = data.property[0].address.locality
print(city)  # -> San Antonio
zip_code = data.property[0].address.postal1
print(zip_code)  # -> 78210
county = data.property[0].area.countrysecsubd
print(county)  # -> Bexar County
# and so on and so forth...
property = data['property'][0]
city = property['address']['locality'] #etc.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM