首页 > 解决方案 > 读取带有嵌套字典的文本文件并转换为 csv

问题描述

文本文件包含嵌套字典,示例数据具有如下列:

{
    '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,深度。卖出.price1,深度。出售.orders1 等作为出售部分。任何帮助。

标签: pythonpython-3.xpandasdataframe

解决方案


这是两者的混合:-

from flatten_json import flatten
import datetime
import pandas as pd

with open('your_file.txt', 'r') as file:
    lists = list(map(eval,file.readlines()))


final = []

def append_final(x):
    global final
    final.append(x)

def parse_list(l):
    for item in l:
        if isinstance(item,dict):
            result=flatten(item,'.')
            append_final(result)
        elif isinstance(item,list):
            parse_list(item)

parse_list(lists)

for f in final:
    for d,dd in f.items():
        print(d,":",dd)

输出:

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
ohlc.high : 789.5
ohlc.low : 772.8
ohlc.close : 784.35
change : 0.0
last_trade_time : 2019-04-04 15:50:29
oi : 0
oi_day_high : 0
oi_day_low : 0
timestamp : 2019-04-05 09:07:00
depth.buy.0.quantity : 43
depth.buy.0.price : 807.9
depth.buy.0.orders : 1
depth.buy.1.quantity : 65
depth.buy.1.price : 795.0
depth.buy.1.orders : 2
depth.buy.2.quantity : 55
depth.buy.2.price : 791.0
depth.buy.2.orders : 1
depth.buy.3.quantity : 25
depth.buy.3.price : 790.1
depth.buy.3.orders : 1
depth.buy.4.quantity : 507
depth.buy.4.price : 42949670.41
depth.buy.4.orders : 12
depth.sell.0.quantity : 114
depth.sell.0.price : 705.95
depth.sell.0.orders : 1
depth.sell.1.quantity : 20
depth.sell.1.price : 760.85
depth.sell.1.orders : 1
depth.sell.2.quantity : 1
depth.sell.2.price : 778.0
depth.sell.2.orders : 1
depth.sell.3.quantity : 1
depth.sell.3.price : 779.0
depth.sell.3.orders : 1
depth.sell.4.quantity : 176
depth.sell.4.price : 42949670.41
depth.sell.4.orders : 6
tradable : True
mode : full
instrument_token : 784129
last_price : 187.2
last_quantity : 1
average_price : 0.0
volume : 7173
buy_quantity : 98533
sell_quantity : 108870
ohlc.open : 188.6
ohlc.high : 189.15
ohlc.low : 183.4
ohlc.close : 187.2
change : 0.0
last_trade_time : 2019-04-04 15:58:40
oi : 0
oi_day_high : 0
oi_day_low : 0
timestamp : 2019-04-05 09:07:00
depth.buy.0.quantity : 2
depth.buy.0.price : 200.0
depth.buy.0.orders : 2
depth.buy.1.quantity : 1
depth.buy.1.price : 199.95
depth.buy.1.orders : 1
depth.buy.2.quantity : 1
depth.buy.2.price : 199.9
depth.buy.2.orders : 1
depth.buy.3.quantity : 1
depth.buy.3.price : 199.85
depth.buy.3.orders : 1
depth.buy.4.quantity : 1901
depth.buy.4.price : 42949670.41
depth.buy.4.orders : 28
depth.sell.0.quantity : 110
depth.sell.0.price : 179.0
depth.sell.0.orders : 1
depth.sell.1.quantity : 50
depth.sell.1.price : 180.0
depth.sell.1.orders : 1
depth.sell.2.quantity : 1
depth.sell.2.price : 181.0
depth.sell.2.orders : 1
depth.sell.3.quantity : 150
depth.sell.3.price : 185.0
depth.sell.3.orders : 1
depth.sell.4.quantity : 421
depth.sell.4.price : 42949670.41
depth.sell.4.orders : 14
tradable : True
mode : full
instrument_token : 215553
last_price : 148.9
last_quantity : 247
average_price : 0.0
volume : 11940
buy_quantity : 106132
sell_quantity : 259400
ohlc.open : 149.8
ohlc.high : 150.75
ohlc.low : 146.5
ohlc.close : 148.9
change : 0.0
last_trade_time : 2019-04-04 15:59:02
oi : 0
oi_day_high : 0
oi_day_low : 0
timestamp : 2019-04-05 09:07:00
depth.buy.0.quantity : 100
depth.buy.0.price : 158.0
depth.buy.0.orders : 1
.....

推荐阅读