读取带有嵌套字典的文本文件并转换为cs

2024-05-21 01:06:58 发布

您现在位置:Python中文网/ 问答频道 /正文

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

{'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等。任何帮助。你知道吗


Tags: datetimebuypricequantitylowlast文本文件orders
3条回答

以下是我对你问题的解决方案:

import datetime


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


final = []

def append_final(x):
    global final
    last = 0
    for name in final:
        if x in name:
            num = ''
            for n in reversed(name):
                if str.isdigit(n):
                    num = n + num
            if num and int(num)>last:
                last = int(num)

    final.append(x+str(last+1))

def parse_dict(d,base=''):
    for key in d.keys():
        if base:
            new_base = base + "." + key
        else:
            new_base = key
        append_final(new_base)
        if isinstance(d[key],dict): 
            parse_dict(d[key],new_base)
        elif isinstance(d[key], list):
            parse_list(d[key],new_base)

def parse_list(l,base=''):
    for item in l:
        if isinstance(item, dict):
            parse_dict(item,base)
        elif isinstance(item, str):
            if base:
                new_base = base+ "." + item
            else:
                new_base = item
            append_final(new_base)


for l in lists:
    parse_list(l)

final.sort()

print(final)

这可能需要一段时间来处理所有的钥匙,请耐心等待。 根据你的需要做一些修改。:)

from flatten_json import flatten
result=flatten(your_dict,'.')
data=pd.DataFrame(result,index=[0])

这应该是你的工作。你知道吗

你知道吗参考资料:你知道吗

  1. flatten_json()

以下是两者的混合物:

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
.....

相关问题 更多 >