I have some data in my variable A:
A = [{'gestionnaire': "AAAAt", 'id': 'Affl.0', 'idObjet': 'BBB', 'nature': '03', 'precisione': '010'
, 'producteur': "CCCC", 'qualiteCategorisation': '01', 'representation': {'href': 'GEOMETRIE.DDD'}, 'reseau': 'DECH', 'thematique
e': '10}]
I can have some access of attribut like id
and producteur
for example.
for elem in A:
print(elem['id'])
but if i want to acess to all data it doesn't work:
for elem in A.values():
print(elem)
Error:
AttributeError: 'list' object has no attribute 'values'
The answer depends on your data structures.
If all are within a single level of list and not like dictionary within list within a dictionary tree. Then,
Use something like this.
A= [{'gestionnaire': "AAAAt", 'id': 'Affl.0', 'idObjet': 'BBB', 'nature': '03', 'precisione': '010'
, 'producteur': "CCCC", 'qualiteCategorisation': '01', 'representation': {'href': 'GEOMETRIE.DDD'}, 'reseau': 'DECH', 'thematique': '10'}]
for list_item in A:
if 'id' in list_item.keys():
print(list_item['id']
This will print all the values:
for x in A:
for elem in x.keys():
print(x[elem])
output:
AAAAt
Affl.0
BBB
03
010
CCCC
01
{'href': 'GEOMETRIE.DDD'}
DECH
10
It looks like either dictionary inside a list or JSON. You can extract the dictionary out of the A
then do your operations
A = [{'gestionnaire': "AAAAt", 'id': 'Affl.0', 'idObjet': 'BBB', 'nature': '03', 'precisione': '010'
, 'producteur': "CCCC", 'qualiteCategorisation': '01', 'representation': {'href': 'GEOMETRIE.DDD'}, 'reseau': 'DECH', 'thematiquee': '10'}]
mydict = A[0]
for elem in mydict.values():
print(elem)
Output:
AAAAt
Affl.0
BBB
03
010
CCCC
01
{'href': 'GEOMETRIE.DDD'}
DECH
10
In case it's JSON data you need to use
A = '''[{"gestionnaire": "AAAAt", "id": "Affl.0", "idObjet": "BBB", "nature": "03", "precisione": "010"
, "producteur": "CCCC", "qualiteCategorisation": "01", "representation": {"href": "GEOMETRIE.DDD"}, "reseau": "DECH", "thematiquee": "10"}]'''
import json
x = json.loads(A)
print(x[0]['id'])
print(x[0]['idObjet'])
Output:
Affl.0
BBB
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.