Click here to Skip to main content
15,740,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the next code

for elem in root:
    for nodo in root.iterfind(".//*[@type_id='1']"):

What I have tried:

With that code the show me this ( it is only a part of the result):

{'id': '1372839298', 'event_id': '3', 'type_id': '1', 'period_id': '1', 'min': '0', 'sec': '1', 'player_id': '37572', 'team_id': '43', 'outcome': '1', 'x': '50.1', 'y': '50.0', 'timestamp': '2011-08-21T16:00:40.179', 'last_modified': '2011-08-21T16:00:41'}
{'id': '978322590', 'event_id': '4', 'type_id': '1', 'period_id': '1', 'min': '0', 'sec': '2', 'player_id': '20664', 'team_id': '43', 'outcome': '1', 'x': '48.2', 'y': '49.1', 'timestamp': '2011-08-21T16:00:41.585', 'last_modified': '2011-08-21T16:00:44'}
{'id': '1962550717', 'event_id': '7', 'type_id': '1', 'period_id': '1', 'min': '0', 'sec': '19', 'player_id': '42593', 'team_id': '43', 'outcome': '0', 'x': '27.8', 'y': '100.0', 'timestamp': '2011-08-21T16:00:58.445', 'last_modified': '2011-08-21T16:01:01'}
{'id': '1886599927', 'event_id': '5', 'type_id': '1', 'period_id': '1', 'min': '0', 'sec': '23', 'player_id': '27696', 'team_id': '30', 'outcome': '1', 'x': '50.9', 'y': '20.0', 'timestamp': '2011-08-21T16:01:02.466', 'last_modified': '2011-08-21T16:01:17'}

and it is okey, previously I created empty list and now I want to extract the values of period_id, min, sec, player_id and team_id and make a table. I know how to filter ( previous code ) but I don t know how to extract this specific variables into a list

my try:

for i, v in enumerate(mydic):
    for j in v:
        if j == 'team_id':
        elif j == 'sec':

Updated 31-Oct-22 6:28am

1 solution

You have all the information in the printed results. The nodo.attrib is a standard Python dictionary of all the attributes of the node. See Built-in Types — Python 3.11.0 documentation[^] for the methods of dictionaries.

Starting with a list of all your dictionary items, something like:
stuff = [{'id': '1372839298', 'event_id': '3', 'type_id': '1', 'period_id': '1', 'min': '0', 'sec': '1', 'player_id': '37572', 'team_id': '43', 'outcome': '1', 'x': '50.1', 'y': '50.0', 'timestamp': '2011-08-21T16:00:40.179', 'last_modified': '2011-08-21T16:00:41'},
         {'id': '978322590', 'event_id': '4', 'type_id': '1', 'period_id': '1', 'min': '0', 'sec': '2', 'player_id': '20664', 'team_id': '43', 'outcome': '1', 'x': '48.2', 'y': '49.1', 'timestamp': '2011-08-21T16:00:41.585', 'last_modified': '2011-08-21T16:00:44'},
         {'id': '1962550717', 'event_id': '7', 'type_id': '1', 'period_id': '1', 'min': '0', 'sec': '19', 'player_id': '42593', 'team_id': '43', 'outcom': '1', 'x': '48.2', 'y': '49.1', 'timestamp': '2011-08-21T16:00:41.585', 'last_modified': '2011-08-21T16:00:44'} ]
newlist = []
keys = [ 'period_id', 'min', 'sec', 'player_id', 'team_id' ]
for d in range(len(keys)):
for dicts in stuff:
    for k in range(len(keys)):

Share this answer
David Vazquez Bande 31-Oct-22 11:06am    
yeah, I know that I have the results but I need to add this results to a empty list
Richard MacCutchan 31-Oct-22 11:09am    
What exactly is the problem in copying those specific items to the new list?
David Vazquez Bande 31-Oct-22 11:10am    
it a list more longer than this, it is only a extract for seeing a example of the output
Richard MacCutchan 31-Oct-22 11:26am    
Fine, but I still have no idea what your problem is. Please use the Improve question link above, and add complete details of what is not working.
David Vazquez Bande 31-Oct-22 11:27am    
I need to extract the values of min, sec ... and put it in different list. Like create a csv

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900