文本文件包含嵌套字典,示例数据的列如下:
{'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打开, olhc.关闭etc和深度转换为深度。买入数量1,深度。买入价格1,深度。买入订单1,用于买入部分和深度。卖出数量1,深度。卖出价格1,深度。卖出。卖出部分的订单1等。任何帮助。你知道吗
以下是我对你问题的解决方案:
这可能需要一段时间来处理所有的钥匙,请耐心等待。 根据你的需要做一些修改。:)
这应该是你的工作。你知道吗
你知道吗参考资料:你知道吗
以下是两者的混合物:
输出:
相关问题 更多 >
编程相关推荐