求大神看下这数据,我如何才能快速得到想要的结果
本帖最后由 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,谢谢!
代码发全 本帖最后由 阿奇_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
data = json.loads(json_str)['result']['data']
df = pd.DataFrame(data)
df
BOARD_CODE TRADE_DATE... LOAN_SELL_AMTLOAN_NETSELL_AMT
0 4652022-12-13 00:00:00...1.626764e+08 16485268.94
1 10402022-12-13 00:00:00...1.060470e+08 36687720.65
2 10272022-12-13 00:00:00...8.512190e+07 30802617.28
3 4362022-12-13 00:00:00...5.104488e+06 2333427.00
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')
本帖最后由 futui 于 2022-12-14 17:27 编辑
阿奇_o 发表于 2022-12-14 16:16
OOP面向对象编程,多少应该学过些了吧。。得用上已学过的知识呀,理论联系实际呀 —— 随时问一下:对象是 ...
非常感谢!!这省力了,我原来准备用 re笨办法,一个项目一个项目的弄{:5_109:} , BOARD_CODE = re.findall(r'BOARD_CODE\":\"(.*?)\",',txt)
页:
[1]