鱼C论坛

 找回密码
 立即注册
查看: 2013|回复: 3

[已解决]求大神看下这数据,我如何才能快速得到想要的结果

[复制链接]
发表于 2022-12-14 11:12:45 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 futui 于 2022-12-14 12:17 编辑

用requests 下载,通过respone.text得到下面的数据:(中间已删除,只保留了三组数据)

datatable1134897({"version":"9cbe70548db011707439d9e37f569cf8","result":{"pages":2,"data":[{"BOARD_CODE":"465","TRADE_DATE":"2022-12-13 00:00:00","BOARD_NAME":"化学制药","FIN_BALANCE":36145706197,"LOAN_BALANCE_VOL":80526578,"FIN_BUY_AMT":3316910198,"LOAN_BALANCE":2057220856.3,"LOAN_SELL_VOL":7625760,"MARGIN_BALANCE":38202927053.3,"FIN_BALANCE_DIFF":34088485340.7,"NOTLIMITED_MARKETCAP_A":1287571223510.34,"FIN_BALANCE_RATIO":2.80727819,"FIN_REPAY_AMT":2744407966,"FIN_NETBUY_AMT":572502232,"LOAN_REPAY_VOL":5689106,"FIN_NETSELL_AMT":1936654,"BOARD_TYPE_CODE":"005","BOARD_TYPE":"财富通行业","LOAN_SELL_AMT":162676378.4,"LOAN_NETSELL_AMT":16485268.94},{"BOARD_CODE":"1040","TRADE_DATE":"2022-12-13 00:00:00","BOARD_NAME":"中药","FIN_BALANCE":24252241318,"LOAN_BALANCE_VOL":37823056,"FIN_BUY_AMT":3731371240,"LOAN_BALANCE":710630287.85,"LOAN_SELL_VOL":7245408,"MARGIN_BALANCE":24962871605.85,"FIN_BALANCE_DIFF":23541611030.15,"NOTLIMITED_MARKETCAP_A":914764092422.24,"FIN_BALANCE_RATIO":2.65120172,"FIN_REPAY_AMT":3470028057,"FIN_NETBUY_AMT":261343183,"LOAN_REPAY_VOL":3810674,"FIN_NETSELL_AMT":3434734,"BOARD_TYPE_CODE":"005","BOARD_TYPE":"财富通行业","LOAN_SELL_AMT":106046966.51,"LOAN_NETSELL_AMT":36687720.65},{"BOARD_CODE":"1027","TRADE_DATE":"2022-12-13 00:00:00","BOARD_NAME":"小金属","FIN_BALANCE":22942666419,"LOAN_BALANCE_VOL":34295458,"FIN_BUY_AMT":1321980753,"LOAN_BALANCE":522776950.16,"LOAN_SELL_VOL":3830860,"MARGIN_BALANCE":23465443369.16,"FIN_BALANCE_DIFF":22419889468.84,"NOTLIMITED_MARKETCAP_A":586676784426.95,"FIN_BALANCE_RATIO":3.91061433,"FIN_REPAY_AMT":1094275804,"FIN_NETBUY_AMT":227704949,"LOAN_REPAY_VOL":2471251,"FIN_NETSELL_AMT":1359609,"BOARD_TYPE_CODE":"005","BOARD_TYPE":"财富通行业","LOAN_SELL_AMT":85121895.77,"LOAN_NETSELL_AMT":30802617.28},{"BOARD_CODE":"436","TRADE_DATE":"2022-12-13 00:00:00","BOARD_NAME":"纺织服装","FIN_BALANCE":5714181733,"LOAN_BALANCE_VOL":8840444,"FIN_BUY_AMT":174305903,"LOAN_BALANCE":56384004.19,"LOAN_SELL_VOL":841110,"MARGIN_BALANCE":5770565737.19,"FIN_BALANCE_DIFF":5657797728.81,"NOTLIMITED_MARKETCAP_A":251268527849.76,"FIN_BALANCE_RATIO":2.27413349,"FIN_REPAY_AMT":183080933,"FIN_NETBUY_AMT":-8775030,"LOAN_REPAY_VOL":504900,"FIN_NETSELL_AMT":336210,"BOARD_TYPE_CODE":"005","BOARD_TYPE":"财富通行业","LOAN_SELL_AMT":5104488,"LOAN_NETSELL_AMT":2333427}],"count":86},"success":true,"message":"ok","code":0});

我想通过“BOARD_NAME”  得到:化学制药,中药,小金属的列表或数据框的列

我想通过“LOAN_BALANCE_VOL”  得到:80526578,37823056,34295458的列表或数据框的列

求大师指点下,这个能用json吗(有代码最好), 还是只能用re,谢谢!



最佳答案
2022-12-14 16:16:22
本帖最后由 阿奇_o 于 2022-12-14 16:24 编辑

OOP面向对象编程,多少应该学过些了吧。。得用上已学过的知识呀,理论联系实际呀 —— 随时问一下:对象是什么(这是什么类型的对象)? 然后 这对象有哪些我可以用的方法?
你这对 response.text  本质不就是 字符串 吗 —— str 类型,一个字符串对象 。  
里面包含有 json字符串的数据结构(json本质也是字符串!),那就把它提取(切片)出来不就得了? —— 不就是最前最后的{}大括号里的字符串串, {...json string...}
所以:
import pandas as pd
import json
txt = '''datatable1134897({"version":"9cbe70548db011707439d9e37f569cf8","result":{"pages":2,"data":[{"BOARD_CODE":"465","TRADE_DATE":"2022-12-13 00:00:00","BOARD_NAME":"化学制药","FIN_BALANCE":36145706197,"LOAN_BALANCE_VOL":80526578,"FIN_BUY_AMT":3316910198,"LOAN_BALANCE":2057220856.3,"LOAN_SELL_VOL":7625760,"MARGIN_BALANCE":38202927053.3,"FIN_BALANCE_DIFF":34088485340.7,"NOTLIMITED_MARKETCAP_A":1287571223510.34,"FIN_BALANCE_RATIO":2.80727819,"FIN_REPAY_AMT":2744407966,"FIN_NETBUY_AMT":572502232,"LOAN_REPAY_VOL":5689106,"FIN_NETSELL_AMT":1936654,"BOARD_TYPE_CODE":"005","BOARD_TYPE":"财富通行业","LOAN_SELL_AMT":162676378.4,"LOAN_NETSELL_AMT":16485268.94},{"BOARD_CODE":"1040","TRADE_DATE":"2022-12-13 00:00:00","BOARD_NAME":"中药","FIN_BALANCE":24252241318,"LOAN_BALANCE_VOL":37823056,"FIN_BUY_AMT":3731371240,"LOAN_BALANCE":710630287.85,"LOAN_SELL_VOL":7245408,"MARGIN_BALANCE":24962871605.85,"FIN_BALANCE_DIFF":23541611030.15,"NOTLIMITED_MARKETCAP_A":914764092422.24,"FIN_BALANCE_RATIO":2.65120172,"FIN_REPAY_AMT":3470028057,"FIN_NETBUY_AMT":261343183,"LOAN_REPAY_VOL":3810674,"FIN_NETSELL_AMT":3434734,"BOARD_TYPE_CODE":"005","BOARD_TYPE":"财富通行业","LOAN_SELL_AMT":106046966.51,"LOAN_NETSELL_AMT":36687720.65},{"BOARD_CODE":"1027","TRADE_DATE":"2022-12-13 00:00:00","BOARD_NAME":"小金属","FIN_BALANCE":22942666419,"LOAN_BALANCE_VOL":34295458,"FIN_BUY_AMT":1321980753,"LOAN_BALANCE":522776950.16,"LOAN_SELL_VOL":3830860,"MARGIN_BALANCE":23465443369.16,"FIN_BALANCE_DIFF":22419889468.84,"NOTLIMITED_MARKETCAP_A":586676784426.95,"FIN_BALANCE_RATIO":3.91061433,"FIN_REPAY_AMT":1094275804,"FIN_NETBUY_AMT":227704949,"LOAN_REPAY_VOL":2471251,"FIN_NETSELL_AMT":1359609,"BOARD_TYPE_CODE":"005","BOARD_TYPE":"财富通行业","LOAN_SELL_AMT":85121895.77,"LOAN_NETSELL_AMT":30802617.28},{"BOARD_CODE":"436","TRADE_DATE":"2022-12-13 00:00:00","BOARD_NAME":"纺织服装","FIN_BALANCE":5714181733,"LOAN_BALANCE_VOL":8840444,"FIN_BUY_AMT":174305903,"LOAN_BALANCE":56384004.19,"LOAN_SELL_VOL":841110,"MARGIN_BALANCE":5770565737.19,"FIN_BALANCE_DIFF":5657797728.81,"NOTLIMITED_MARKETCAP_A":251268527849.76,"FIN_BALANCE_RATIO":2.27413349,"FIN_REPAY_AMT":183080933,"FIN_NETBUY_AMT":-8775030,"LOAN_REPAY_VOL":504900,"FIN_NETSELL_AMT":336210,"BOARD_TYPE_CODE":"005","BOARD_TYPE":"财富通行业","LOAN_SELL_AMT":5104488,"LOAN_NETSELL_AMT":2333427}],"count":86},"success":true,"message":"ok","code":0});'''
json_str = txt[txt.index('{') : txt.rindex(')')]
data = json.loads(json_str)['result']['data']
df = pd.DataFrame(data)
df
  BOARD_CODE           TRADE_DATE  ... LOAN_SELL_AMT  LOAN_NETSELL_AMT
0        465  2022-12-13 00:00:00  ...  1.626764e+08       16485268.94
1       1040  2022-12-13 00:00:00  ...  1.060470e+08       36687720.65
2       1027  2022-12-13 00:00:00  ...  8.512190e+07       30802617.28
3        436  2022-12-13 00:00:00  ...  5.104488e+06        2333427.00

[4 rows x 20 columns]
df.columns
Index(['BOARD_CODE', 'TRADE_DATE', 'BOARD_NAME', 'FIN_BALANCE',
       'LOAN_BALANCE_VOL', 'FIN_BUY_AMT', 'LOAN_BALANCE', 'LOAN_SELL_VOL',
       'MARGIN_BALANCE', 'FIN_BALANCE_DIFF', 'NOTLIMITED_MARKETCAP_A',
       'FIN_BALANCE_RATIO', 'FIN_REPAY_AMT', 'FIN_NETBUY_AMT',
       'LOAN_REPAY_VOL', 'FIN_NETSELL_AMT', 'BOARD_TYPE_CODE', 'BOARD_TYPE',
       'LOAN_SELL_AMT', 'LOAN_NETSELL_AMT'],
      dtype='object')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-14 16:08:29 | 显示全部楼层
代码发全
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-14 16:16:22 | 显示全部楼层    本楼为最佳答案   
本帖最后由 阿奇_o 于 2022-12-14 16:24 编辑

OOP面向对象编程,多少应该学过些了吧。。得用上已学过的知识呀,理论联系实际呀 —— 随时问一下:对象是什么(这是什么类型的对象)? 然后 这对象有哪些我可以用的方法?
你这对 response.text  本质不就是 字符串 吗 —— str 类型,一个字符串对象 。  
里面包含有 json字符串的数据结构(json本质也是字符串!),那就把它提取(切片)出来不就得了? —— 不就是最前最后的{}大括号里的字符串串, {...json string...}
所以:
import pandas as pd
import json
txt = '''datatable1134897({"version":"9cbe70548db011707439d9e37f569cf8","result":{"pages":2,"data":[{"BOARD_CODE":"465","TRADE_DATE":"2022-12-13 00:00:00","BOARD_NAME":"化学制药","FIN_BALANCE":36145706197,"LOAN_BALANCE_VOL":80526578,"FIN_BUY_AMT":3316910198,"LOAN_BALANCE":2057220856.3,"LOAN_SELL_VOL":7625760,"MARGIN_BALANCE":38202927053.3,"FIN_BALANCE_DIFF":34088485340.7,"NOTLIMITED_MARKETCAP_A":1287571223510.34,"FIN_BALANCE_RATIO":2.80727819,"FIN_REPAY_AMT":2744407966,"FIN_NETBUY_AMT":572502232,"LOAN_REPAY_VOL":5689106,"FIN_NETSELL_AMT":1936654,"BOARD_TYPE_CODE":"005","BOARD_TYPE":"财富通行业","LOAN_SELL_AMT":162676378.4,"LOAN_NETSELL_AMT":16485268.94},{"BOARD_CODE":"1040","TRADE_DATE":"2022-12-13 00:00:00","BOARD_NAME":"中药","FIN_BALANCE":24252241318,"LOAN_BALANCE_VOL":37823056,"FIN_BUY_AMT":3731371240,"LOAN_BALANCE":710630287.85,"LOAN_SELL_VOL":7245408,"MARGIN_BALANCE":24962871605.85,"FIN_BALANCE_DIFF":23541611030.15,"NOTLIMITED_MARKETCAP_A":914764092422.24,"FIN_BALANCE_RATIO":2.65120172,"FIN_REPAY_AMT":3470028057,"FIN_NETBUY_AMT":261343183,"LOAN_REPAY_VOL":3810674,"FIN_NETSELL_AMT":3434734,"BOARD_TYPE_CODE":"005","BOARD_TYPE":"财富通行业","LOAN_SELL_AMT":106046966.51,"LOAN_NETSELL_AMT":36687720.65},{"BOARD_CODE":"1027","TRADE_DATE":"2022-12-13 00:00:00","BOARD_NAME":"小金属","FIN_BALANCE":22942666419,"LOAN_BALANCE_VOL":34295458,"FIN_BUY_AMT":1321980753,"LOAN_BALANCE":522776950.16,"LOAN_SELL_VOL":3830860,"MARGIN_BALANCE":23465443369.16,"FIN_BALANCE_DIFF":22419889468.84,"NOTLIMITED_MARKETCAP_A":586676784426.95,"FIN_BALANCE_RATIO":3.91061433,"FIN_REPAY_AMT":1094275804,"FIN_NETBUY_AMT":227704949,"LOAN_REPAY_VOL":2471251,"FIN_NETSELL_AMT":1359609,"BOARD_TYPE_CODE":"005","BOARD_TYPE":"财富通行业","LOAN_SELL_AMT":85121895.77,"LOAN_NETSELL_AMT":30802617.28},{"BOARD_CODE":"436","TRADE_DATE":"2022-12-13 00:00:00","BOARD_NAME":"纺织服装","FIN_BALANCE":5714181733,"LOAN_BALANCE_VOL":8840444,"FIN_BUY_AMT":174305903,"LOAN_BALANCE":56384004.19,"LOAN_SELL_VOL":841110,"MARGIN_BALANCE":5770565737.19,"FIN_BALANCE_DIFF":5657797728.81,"NOTLIMITED_MARKETCAP_A":251268527849.76,"FIN_BALANCE_RATIO":2.27413349,"FIN_REPAY_AMT":183080933,"FIN_NETBUY_AMT":-8775030,"LOAN_REPAY_VOL":504900,"FIN_NETSELL_AMT":336210,"BOARD_TYPE_CODE":"005","BOARD_TYPE":"财富通行业","LOAN_SELL_AMT":5104488,"LOAN_NETSELL_AMT":2333427}],"count":86},"success":true,"message":"ok","code":0});'''
json_str = txt[txt.index('{') : txt.rindex(')')]
data = json.loads(json_str)['result']['data']
df = pd.DataFrame(data)
df
  BOARD_CODE           TRADE_DATE  ... LOAN_SELL_AMT  LOAN_NETSELL_AMT
0        465  2022-12-13 00:00:00  ...  1.626764e+08       16485268.94
1       1040  2022-12-13 00:00:00  ...  1.060470e+08       36687720.65
2       1027  2022-12-13 00:00:00  ...  8.512190e+07       30802617.28
3        436  2022-12-13 00:00:00  ...  5.104488e+06        2333427.00

[4 rows x 20 columns]
df.columns
Index(['BOARD_CODE', 'TRADE_DATE', 'BOARD_NAME', 'FIN_BALANCE',
       'LOAN_BALANCE_VOL', 'FIN_BUY_AMT', 'LOAN_BALANCE', 'LOAN_SELL_VOL',
       'MARGIN_BALANCE', 'FIN_BALANCE_DIFF', 'NOTLIMITED_MARKETCAP_A',
       'FIN_BALANCE_RATIO', 'FIN_REPAY_AMT', 'FIN_NETBUY_AMT',
       'LOAN_REPAY_VOL', 'FIN_NETSELL_AMT', 'BOARD_TYPE_CODE', 'BOARD_TYPE',
       'LOAN_SELL_AMT', 'LOAN_NETSELL_AMT'],
      dtype='object')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-12-14 17:18:06 | 显示全部楼层
本帖最后由 futui 于 2022-12-14 17:27 编辑
阿奇_o 发表于 2022-12-14 16:16
OOP面向对象编程,多少应该学过些了吧。。得用上已学过的知识呀,理论联系实际呀 —— 随时问一下:对象是 ...


非常感谢!!这省力了,我原来准备用 re笨办法,一个项目一个项目的弄 , BOARD_CODE = re.findall(r'BOARD_CODE\":\"(.*?)\",',txt)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-15 21:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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