V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
pc10201
V2EX  ›  Python

如何用json来分割

  •  
  •   pc10201 · 2013-12-20 11:37:02 +08:00 · 7189 次点击
    这是一个创建于 4025 天前的主题,其中的信息可能已经有所发展或是发生改变。
    采集新浪股票的数据
    http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=1&num=10&sort=changepercent&asc=0&node=sz_a&symbol=

    结果是
    [{symbol:"sz002632",code:"002632",name:"道明光学",trade:"11.830",pricechange:"1.080",changepercent:"10.047",buy:"11.830",sell:"0.000",settlement:"10.750",open:"11.050",high:"11.830",low:"11.050",volume:"5738784",amount:"67506360",ticktime:"10:17:24",per:31.973,pb:1.827,mktcap:164047.793,nmc:59632.0725,turnoverratio:11.38478},{symbol:"sz300076",code:"300076",name:"GQY视讯",trade:"22.080",pricechange:"2.010",changepercent:"10.015",buy:"22.080",sell:"0.000",settlement:"20.070",open:"19.760",high:"22.080",low:"19.750",volume:"2770681",amount:"59061084",ticktime:"10:17:24",per:71.226,pb:2.186,mktcap:234048,nmc:179340.964704,turnoverratio:3.41119},{symbol:"sz002577",code:"002577",name:"雷柏科技",trade:"12.420",pricechange:"1.130",changepercent:"10.009",buy:"12.420",sell:"0.000",settlement:"11.290",open:"12.420",high:"12.420",low:"12.420",volume:"172540",amount:"2142947",ticktime:"10:17:24",per:36.529,pb:2.512,mktcap:351336.96,nmc:87834.24,turnoverratio:0.24398},{symbol:"sz002240",code:"002240",name:"威华股份",trade:"8.470",pricechange:"0.770",changepercent:"10.000",buy:"8.470",sell:"0.000",settlement:"7.700",open:"8.470",high:"8.470",low:"8.470",volume:"2524799",amount:"21385048",ticktime:"10:17:24",per:-35.292,pb:2.659,mktcap:415626.288,nmc:249231.314409,turnoverratio:0.85804},{symbol:"sz300269",code:"300269",name:"联建光电",trade:"18.950",pricechange:"1.720",changepercent:"9.983",buy:"18.950",sell:"0.000",settlement:"17.230",open:"18.880",high:"18.950",low:"18.010",volume:"3610133",amount:"67796368",ticktime:"10:17:24",per:51.216,pb:3.884,mktcap:223094.56,nmc:110361.246875,turnoverratio:6.19892},{symbol:"sz300006",code:"300006",name:"莱美药业",trade:"26.830",pricechange:"1.990",changepercent:"8.011",buy:"26.830",sell:"26.880",settlement:"24.840",open:"25.000",high:"27.320",low:"24.850",volume:"2288823",amount:"60361640",ticktime:"10:17:24",per:92.517,pb:5.009,mktcap:541412.650031,nmc:312658.717799,turnoverratio:1.96409},{symbol:"sz002174",code:"002174",name:"梅 花 伞",trade:"48.900",pricechange:"3.360",changepercent:"7.378",buy:"48.890",sell:"48.900",settlement:"45.540",open:"45.750",high:"49.000",low:"45.700",volume:"1338748",amount:"63857448",ticktime:"10:17:24",per:828.814,pb:18.276,mktcap:405576.21369,nmc:405576.21369,turnoverratio:1.61412},{symbol:"sz300236",code:"300236",name:"上海新阳",trade:"40.500",pricechange:"2.620",changepercent:"6.917",buy:"40.500",sell:"40.590",settlement:"37.880",open:"37.570",high:"40.970",low:"37.210",volume:"1101917",amount:"43087972",ticktime:"10:17:24",per:86.17,pb:5.961,mktcap:460890,nmc:87075,turnoverratio:5.1252},{symbol:"sz002634",code:"002634",name:"棒杰股份",trade:"15.220",pricechange:"0.960",changepercent:"6.732",buy:"15.210",sell:"15.220",settlement:"14.260",open:"15.670",high:"15.690",low:"14.950",volume:"6943193",amount:"107920056",ticktime:"10:17:24",per:38.05,pb:3.116,mktcap:152276.1,nmc:53901.63,turnoverratio:19.60523},{symbol:"sz300276",code:"300276",name:"三丰智能",trade:"14.280",pricechange:"0.810",changepercent:"6.013",buy:"14.280",sell:"14.310",settlement:"13.470",open:"13.370",high:"14.360",low:"13.100",volume:"8006739",amount:"109766856",ticktime:"10:17:24",per:24.621,pb:3.283,mktcap:178214.4,nmc:65093.303688,turnoverratio:17.56498}]
    如何用json先生成列表然后列表项再变成字典或json数据?
    9 条回复    1970-01-01 08:00:00 +08:00
    mengzhuo
        1
    mengzhuo  
       2013-12-20 11:51:15 +08:00
    我竟然看成了……如何用json先生/成列表/然后列表项再变成字典或json数据?

    这本身就是个js可读的列表,列表里是object,不明白LZ要干什么
    justfly
        2
    justfly  
       2013-12-20 12:08:40 +08:00
    import json
    data_list=json.loads("xxxxx")
    for data_item_dict in data_list:
    # do something
    pc10201
        3
    pc10201  
    OP
       2013-12-20 12:18:12 +08:00
    @justfly

    #coding=utf-8
    import json
    import requests


    r=requests.get('http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=1&num=10&sort=changepercent&asc=0&node=sz_a&symbol=')
    print r.text
    data_list=json.loads(r.text)
    for data_item_dict in data_list:
    print data_item_dict

    不行啊,代码报错
    Traceback (most recent call last):
    File "D:/python_project/myself/���˹�Ʊ/test.py", line 8, in <module>
    data_list=json.loads(r.text)
    File "D:\Python27\lib\json\__init__.py", line 338, in loads
    return _default_decoder.decode(s)
    File "D:\Python27\lib\json\decoder.py", line 365, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    File "D:\Python27\lib\json\decoder.py", line 381, in raw_decode
    obj, end = self.scan_once(s, idx)
    ValueError: Expecting property name enclosed in double quotes: line 1 column 3 (char 2)
    Ever
        4
    Ever  
       2013-12-20 12:29:11 +08:00
    这个json不太标准, 比如key没加引号, js处理没问题, 其他语言就看json库是不是兼容非标准了。

    你可以拿pyv8封一个函数, 把这个字符窜当object传进去做一个JSON.stringify, 再吐给python的json库解。
    9hills
        5
    9hills  
       2013-12-20 12:39:46 +08:00
    这个不符合JSON标准,需要单独处理的。
    Jowei
        6
    Jowei  
       2013-12-20 22:23:30 +08:00
    你把代码总request.text 改成 request.content 试试? 我记得requests 返回后,有不同的格式。其中content好像是unicode格式, 再用json.loads()试下。
    pc10201
        7
    pc10201  
    OP
       2013-12-21 11:30:03 +08:00
    @Jowei 也不行
    pc10201
        8
    pc10201  
    OP
       2013-12-21 11:38:19 +08:00
    @Ever 谢谢提醒,我对json还不太熟悉,后来用正则表达式提取出来了~
    stc
        9
    stc  
       2013-12-31 09:04:43 +08:00
    也可以先用正则加上引号,再load
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5428 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:37 · PVG 16:37 · LAX 00:37 · JFK 03:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.