鱼C论坛

 找回密码
 立即注册
查看: 1875|回复: 1

[已解决]单引字典变数据帧的问题,求大师帮忙

[复制链接]
发表于 2023-4-11 23:51:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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)

我代码运行错误
最佳答案
2023-4-11 23:55:11
本帖最后由 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 字符串。
  • 现在您应该可以正常运行代码并获得预期结果。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-11 23:55:11 | 显示全部楼层    本楼为最佳答案   
本帖最后由 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 字符串。
  • 现在您应该可以正常运行代码并获得预期结果。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-14 20:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表