|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
data = '''Query112307719530320351087_1671530759948({"rc":0,"rt":22,"svr":181216787,"lt":1,"full":0,"dlmkts":"","data":{"code":"000001","market":1,"name":"上证指数","klines":["2022-11-10,-34451102208.0,28253458432.0,6197641216.0,-18004312064.0,-16446790144.0,-4.09,3.35,0.74,-2.14,-1.95,3036.13,-0.39,10908.55,-1.33","2022-11-11,2016566784.0,12980424704.0,-14996992000.0,-10999246848.0,13015813632.0,0.16,1.06,-1.23,-0.90,1.06,3087.29,1.69,11139.77,2.12","2022-11-14,-30351907584.0,23198273536.0,7153635328.0,-14855168000.0,-15496739584.0,-2.84,2.17,0.67,-1.39,-1.45,3083.40,-0.13,11113.46,-0.24]}});'''
data_str = re.findall(r'(\{.*\})',response)[0] #提出数据
data_json = json.loads(data_str)['data']['klines']
data_list = data_json.split('","',') #?????
df = pd.DataFrame([data_json])
print(df)
出来只一列,我需要以日期为界成一个列表,出现三行多列
即要把日期前面","》这个为分界,行成多个列表
请大师指导修改代码,谢谢
从给定的数据来看,您需要将data_json变量中的字符串分割成多个列表,并将这些列表作为DataFrame的行。这是一个修改后的代码:
import re
import json
import pandas as pd
data = '''Query112307719530320351087_1671530759948({"rc":0,"rt":22,"svr":181216787,"lt":1,"full":0,"dlmkts":"","data":{"code":"000001","market":1,"name":"上证指数","klines":["2022-11-10,-34451102208.0,28253458432.0,6197641216.0,-18004312064.0,-16446790144.0,-4.09,3.35,0.74,-2.14,-1.95,3036.13,-0.39,10908.55,-1.33","2022-11-11,2016566784.0,12980424704.0,-14996992000.0,-10999246848.0,13015813632.0,0.16,1.06,-1.23,-0.90,1.06,3087.29,1.69,11139.77,2.12","2022-11-14,-30351907584.0,23198273536.0,7153635328.0,-14855168000.0,-15496739584.0,-2.84,2.17,0.67,-1.39,-1.45,3083.40,-0.13,11113.46,-0.24]}});'''
data_str = re.findall(r'(\{.*\})', data)[0] # 提出数据
data_json = json.loads(data_str)['data']['klines']
# 分割字符串,创建多个列表
data_list = [x.split(',') for x in data_json]
# 将列表作为DataFrame的行
df = pd.DataFrame(data_list)
print(df)
现在,您的DataFrame将包含3行和15列,如您所期望的。
|
|