Select to view content in your preferred language

Python List object to varaibles.

709
3
Jump to solution
01-21-2024 09:23 AM
kapalczynski
Frequent Contributor

I have this List object and need to massage it a couple ways... 

First I would like it to read without the {'attributes':  at the front and the trailing }

Like this: 

{'OBJECTID': 2, 'PERMITTEE': None, 'ROUTE': None, 'GLOBALID': '{8BF971DC-473B-4C88-8007-363EDAE7D091}', 'RELGLOBALID': '{F06AD758-9BBB-4096-942D-484F9F2B0435}', 'INSPECTOR_NAME': None, 'PERMIT_INTERNAL_ID': None}, 'geometry': {'x': -77.393420655, 'y': 37.615990732}

Second I would like to be able to set the OBJECTID, PERMITID, ROUTE etc to variables... 

Thoughts?

 

{'attributes': {'OBJECTID': 2, 'PERMITTEE': None, 'ROUTE': None, 'GLOBALID': '{8BF971DC-473B-4C88-8007-363EDAE7D091}', 'RELGLOBALID': '{F06AD758-9BBB-4096-942D-484F9F2B0435}', 'INSPECTOR_NAME': None, 'PERMIT_INTERNAL_ID': None}, 'geometry': {'x': -77.393420655, 'y': 37.615990732}}

 

 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

 

dictionary... not list, check python documentation for more information.  Dictionaries have 'keys' and 'values' and they can be 'sliced'

d = {'attributes': {'OBJECTID': 2, 'PERMITTEE': None, 'ROUTE': None, 'GLOBALID': '{8BF971DC-473B-4C88-8007-363EDAE7D091}', 'RELGLOBALID': '{F06AD758-9BBB-4096-942D-484F9F2B0435}', 'INSPECTOR_NAME': None, 'PERMIT_INTERNAL_ID': None}, 'geometry': {'x': -77.393420655, 'y': 37.615990732}}

d.keys()
Out[2]: dict_keys(['attributes', 'geometry'])

d['attributes']  # -- slice by the key
 
{'OBJECTID': 2,
 'PERMITTEE': None,
 'ROUTE': None,
 'GLOBALID': '{8BF971DC-473B-4C88-8007-363EDAE7D091}',
 'RELGLOBALID': '{F06AD758-9BBB-4096-942D-484F9F2B0435}',
 'INSPECTOR_NAME': None,
 'PERMIT_INTERNAL_ID': None}

d['attributes']['GLOBALID']
'{8BF971DC-473B-4C88-8007-363EDAE7D091}'

... sort of retired...

View solution in original post

3 Replies
kapalczynski
Frequent Contributor

I guess my question is how to I access the Attribute Key and the Geometry Key and write their objects to variables.

This is what I am doing right now and ... querying a dataset and writing to json file and trying to read that and put into variables 

unless there is a better way?

    URL = targetURL + '/query'
    field = 'OBJECTID'
    whereclause = 'OBJECTID=2'
    PARAMS = {'token': portaltoken,'f': 'json', 'where': '{}'.format(whereclause), 'returnIdsOnly': False, 'returnCountOnly': False, 'returnGeometry': True,
                  'outFields': '*'}
    r = requests.post(url=URL, data=PARAMS)

    outjsonpath = outputVariable + "output.json"
    layer_data = r.json()
    layer_data_final = layer_data
    print('Writing JSON file...')
    with open(outjsonpath, 'w') as out_json_file:
        json.dump(layer_data_final, out_json_file)

    filetoImport = outjsonpath
    f = open(filetoImport)
    data = json.load(f)
    featureAddingAdd = data['features']

    print(featureAddingAdd)

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

 

dictionary... not list, check python documentation for more information.  Dictionaries have 'keys' and 'values' and they can be 'sliced'

d = {'attributes': {'OBJECTID': 2, 'PERMITTEE': None, 'ROUTE': None, 'GLOBALID': '{8BF971DC-473B-4C88-8007-363EDAE7D091}', 'RELGLOBALID': '{F06AD758-9BBB-4096-942D-484F9F2B0435}', 'INSPECTOR_NAME': None, 'PERMIT_INTERNAL_ID': None}, 'geometry': {'x': -77.393420655, 'y': 37.615990732}}

d.keys()
Out[2]: dict_keys(['attributes', 'geometry'])

d['attributes']  # -- slice by the key
 
{'OBJECTID': 2,
 'PERMITTEE': None,
 'ROUTE': None,
 'GLOBALID': '{8BF971DC-473B-4C88-8007-363EDAE7D091}',
 'RELGLOBALID': '{F06AD758-9BBB-4096-942D-484F9F2B0435}',
 'INSPECTOR_NAME': None,
 'PERMIT_INTERNAL_ID': None}

d['attributes']['GLOBALID']
'{8BF971DC-473B-4C88-8007-363EDAE7D091}'

... sort of retired...
kapalczynski
Frequent Contributor

@DanPatterson  Cheers.... thanks for the correction and guidance... much appreciated.

0 Kudos