Текстовый файл содержит вложенный словарь, а примеры данных имеют следующие столбцы:
{'tradable': True,
'mode': 'full',
'instrument_token': 70401,
'last_price': 784.35,
'last_quantity': 10,
'average_price': 0.0,
'volume': 2301,
'buy_quantity': 22208,
'sell_quantity': 54655,
'ohlc': {'open': 788.9, 'high': 789.5, 'low': 772.8, 'close': 784.35},
'change': 0.0,
'last_trade_time': datetime.datetime(2019, 4, 4, 15, 50, 29),
'oi': 0,
'oi_day_high': 0,
'oi_day_low': 0,
'timestamp': datetime.datetime(2019, 4, 5, 9, 7),
'depth':
{'buy': [{'quantity': 43, 'price': 807.9, 'orders': 1}, {'quantity': 65, 'price': 795.0, 'orders': 2}, {'quantity': 55, 'price': 791.0, 'orders': 1}, {'quantity': 25, 'price': 790.1, 'orders': 1}, {'quantity': 507, 'price': 42949670.41, 'orders': 12}],
'sell': [{'quantity': 114, 'price': 705.95, 'orders': 1}, {'quantity': 20, 'price': 760.85, 'orders': 1}, {'quantity': 1, 'price': 778.0, 'orders': 1}, {'quantity': 1, 'price': 779.0, 'orders': 1}, {'quantity': 176, 'price': 42949670.41, 'orders': 6}]}}
Мне нужно прочитать текстовый файл и преобразовать данные в CSV-файл с полем, как показано ниже:
columns =
[
'tradable',
'mode',
'instrument_token',
'last_price',
'last_quantity',
'average_price',
'volume',
'buy_quantity',
'sell_quantity',
'ohlc.open',
'ohlc.high',
'ohlc.low',
'ohlc.close',
'change',
'last_trade_time',
'oi',
'oi_day_high',
'oi_day_low',
'timestamp',
'depth.buy.quantity1',
'depth.buy.price1',
'depth.buy.orders1',
'depth.buy.quantity2',
'depth.buy.price2',
'depth.buy.orders2',
'depth.buy.quantity3',
'depth.buy.price3',
'depth.buy.orders3',
'depth.buy.quantity4',
'depth.buy.price4',
'depth.buy.orders4',
'depth.buy.quantity5',
'depth.buy.price5',
'depth.buy.orders5',
'depth.sell.quantity1',
'depth.sell.price1',
'depth.sell.orders1',
'depth.sell.quantity2',
'depth.sell.price2',
'depth.sell.orders2',
'depth.sell.quantity3',
'depth.sell.price3',
'depth.sell.orders3',
'depth.sell.quantity4',
'depth.sell.price4',
'depth.sell.orders4',
'depth.sell.quantity5',
'depth.sell.price5',
'depth.sell.orders5',
]
Пример файла текстового файла прилагается здесь для справки:
data.txt
Вложенные столбцы в olhc должны быть переименованы в olhc.open, olhc.close и т. Д. И глубина для преобразования в глубину. покупка .quantity1, глубина. покупка .price1, глубина. покупка .orders1, для доли на покупку и глубины. продажа .quantity1, глубина. продажа . цена1, глубина. продажа . заказ1 и т. д. для продажи части. Любая помощь.