|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 futui 于 2023-4-11 23:54 编辑
data= ‘’‘var returnjson= {data:[["30532593","安w安","80016241","中金公司","920007,920927","中金恒瑞债券A,中金恒瑞债券C","496","4.31%","920007","中金恒瑞债券A","21.89亿元","4.31%"],["30777698","艾妮","80000222","华夏基金","002011,010692,010693","华夏红利混合,华夏核心价值混合A,华夏核心价值混合C","223","-2.73%","010692","华夏核心价值混合A","60.93亿元","-2.73%"],["30467905","李aw海","80000224","国泰基金","005970,020018,519020,519022","国泰优选股票,国泰金鹿混合,国泰金泰灵活配置混合A,国泰金泰灵活配置混合C","2498","131.58%","020018","国泰金鹿混合","5.45亿元","131.58%"]],record:3388,pages:4,curpage:1}’‘’
data_str = str(response[response.index('{') :])
data_str1 = re.sub(r"\'(.*?)\'", r'"\1"', data_str)
data_json = json.loads(data_str1)['data']
df = pd.DataFrame(data_json)
print(df)
我代码运行错误
本帖最后由 isdkz 于 2023-4-11 23:56 编辑
您的代码有一些小问题。根据您给出的数据,我为您修改了代码,以下是修改后的代码:
import re
import json
import pandas as pd
data = '''var returnjson= {data:[["30532593","安安","80016241","中金公司","920007,920927","中金恒瑞债券A,中金恒瑞债券C","496","4.31%","920007","中金恒瑞债券A","21.89亿元","4.31%"],["30777698","艾邦妮","80000222","华夏基金","002011,010692,010693","华夏红利混合,华夏核心价值混合A,华夏核心价值混合C","223","-2.73%","010692","华夏核心价值混合A","60.93亿元","-2.73%"],["30467905","李海","80000224","国泰基金","005970,020018,519020,519022","国泰消费优选股票,国泰金鹿混合,国泰金泰灵活配置混合A,国泰金泰灵活配置混合C","2498","131.58%","020018","国泰金鹿混合","5.45亿元","131.58%"]],record:3388,pages:4,curpage:1}'''
# 提取 JSON 数据
data_str = data[data.index('{'): data.rindex('}') + 1]
# 使用正则表达式替换单引号为双引号
data_str1 = re.sub(r"(\w+):", r'"\1":', data_str)
# 将 JSON 字符串转换为 Python 字典
data_json = json.loads(data_str1)['data']
# 将数据转换为 DataFrame
df = pd.DataFrame(data_json)
print(df)
我对代码做了以下修改:
- 修改了正则表达式,用于将不带引号的键名添加双引号,以生成有效的 JSON 字符串。
- 现在您应该可以正常运行代码并获得预期结果。
|
|