简体   繁体   中英

Reordering dictionaries keys in a list of dictionaries

Currently, I am working with an API that returns data in this format: I assigned it to a variable quote_data =

{'candles': [{'close': 319.88,
              'datetime': 1549000800000,
              'high': 324.24,
              'low': 288.7701,
              'open': 305.42,
              'volume': 128550254},
             {'close': 279.86,
              'datetime': 1551420000000,
              'high': 307.13,
              'low': 254.46,
              'open': 306.94,
              'volume': 213788920},
             {'close': 238.69,
              'datetime': 1554094800000,
              'high': 296.17,
              'low': 231.13,
              'open': 282.62,
              'volume': 230757870},
             {'close': 185.16,
              'datetime': 1556686800000,
              'high': 258.3499,
              'low': 184.1,
              'open': 238.85,
              'volume': 282598840},
             {'close': 223.46,
              'datetime': 1559365200000,
              'high': 234.74,
              'low': 176.9919,
              'open': 185.51,
              'volume': 214970560},
             {'close': 241.61,
              'datetime': 1561957200000,
              'high': 266.07,
              'low': 222.22,
              'open': 230.21,
              'volume': 199371370},
             {'close': 225.61,
              'datetime': 1564635600000,
              'high': 244.51,
              'low': 211.0,
              'open': 242.65,
              'volume': 134103755},
             {'close': 240.87,
              'datetime': 1567314000000,
              'high': 253.5,
              'low': 218.36,
              'open': 224.08,
              'volume': 136563110},
             {'close': 314.92,
              'datetime': 1569906000000,
              'high': 340.84,
              'low': 224.28,
              'open': 241.5,
              'volume': 235119040},
             {'close': 329.94,
              'datetime': 1572584400000,
              'high': 361.2,
              'low': 309.26,
              'open': 316.32,
              'volume': 157892380},
             {'close': 418.33,
              'datetime': 1575180000000,
              'high': 435.31,
              'low': 327.25,
              'open': 329.4,
              'volume': 207390450},
             {'close': 542.2199,
              'datetime': 1577858400000,
              'high': 547.41,
              'low': 421.71,
              'open': 424.5,
              'volume': 247878770}],
 'empty': False,
 'symbol': 'TSLA'}

I am only interested in the 'candles' portion of this dictionary which leaves me with a list of dictionaries. However to be inserted into a database, I need to:

  1. Insert 2 new dictionary keys for each dictionary:

{'candle': "I need to enumerate this key for each dictionary (Ex: 1, 2, 3, 4)"}

and

{'symbol':'TSLA'}

  1. Then I need to reorder the dictionary according to its keys like this for each dictionary:

[{'candle', 'symbol', 'datetime', 'close', 'high', 'low', 'open', 'volume'}]

  1. Convert it to a list of tuples which I have been using this code to do that:

[tuple(v) for v in map(dict.values, quote_data['candles'])]

This gives me a list of tuples, containing the values of each dictionary.

I have tried several different methods of reordering the dictionaries, but with no success. If anyone is able to help, that would be a tremendous help!

Instead of trying to order the dictionaries, just turn it into a list of two element tuples with the order you want:

rows = [[
      ('candle', index),
      ('symbol', response['symbol']),
      ('datetime', d['datetime']),
      ('close', d['close']),
      ('high', d['high']),
      ('low', d['low']),
      ('open', d['open']),
      ('volume', d['volume']),
    ] for index, d in enumerate(response['candles']) ]
print(*rows, sep='\n')

Output:

[('candle', 0), ('symbol', 'TSLA'), ('datetime', 1549000800000), ('close', 319.88), ('high', 324.24), ('low', 288.7701), ('open', 305.42), ('volume', 128550254)]
[('candle', 1), ('symbol', 'TSLA'), ('datetime', 1551420000000), ('close', 279.86), ('high', 307.13), ('low', 254.46), ('open', 306.94), ('volume', 213788920)]
[('candle', 2), ('symbol', 'TSLA'), ('datetime', 1554094800000), ('close', 238.69), ('high', 296.17), ('low', 231.13), ('open', 282.62), ('volume', 230757870)]
[('candle', 3), ('symbol', 'TSLA'), ('datetime', 1556686800000), ('close', 185.16), ('high', 258.3499),('low', 184.1), ('open', 238.85), ('volume', 282598840)]
[('candle', 4), ('symbol', 'TSLA'), ('datetime', 1559365200000), ('close', 223.46), ('high', 234.74), ('low', 176.9919), ('open', 185.51), ('volume', 214970560)]
[('candle', 5), ('symbol', 'TSLA'), ('datetime', 1561957200000), ('close', 241.61), ('high', 266.07), ('low', 222.22), ('open', 230.21), ('volume', 199371370)]
[('candle', 6), ('symbol', 'TSLA'), ('datetime', 1564635600000), ('close', 225.61), ('high', 244.51), ('low', 211.0), ('open', 242.65), ('volume', 134103755)]
[('candle', 7), ('symbol', 'TSLA'), ('datetime', 1567314000000), ('close', 240.87), ('high', 253.5), ('low', 218.36), ('open', 224.08), ('volume', 136563110)]
[('candle', 8), ('symbol', 'TSLA'), ('datetime', 1569906000000), ('close', 314.92), ('high', 340.84), ('low', 224.28), ('open', 241.5), ('volume', 235119040)]
[('candle', 9), ('symbol', 'TSLA'), ('datetime', 1572584400000), ('close', 329.94), ('high', 361.2), ('low', 309.26), ('open', 316.32), ('volume', 157892380)]
[('candle', 10), ('symbol', 'TSLA'), ('datetime', 1575180000000), ('close', 418.33), ('high', 435.31), ('low', 327.25), ('open', 329.4), ('volume', 207390450)]
[('candle', 11), ('symbol', 'TSLA'), ('datetime', 1577858400000), ('close', 542.2199), ('high', 547.41), ('low', 421.71), ('open', 424.5), ('volume', 247878770)]

Most databases should take a list of tuples.

If not then just turn it back into dicts, as long as you use Python3.7+ it will retain its order:

rows = [dict(row) for row in rows]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM