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
moxiaowei
V2EX  ›  Python

字符串转化问题

  •  
  •   moxiaowei · 2018-10-18 17:22:17 +08:00 · 1804 次点击
    这是一个创建于 2217 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比较喜欢玩梦幻西游,最近准备买号,自己看梦幻藏宝阁呢 又没空,就想着用 scrapy 抓点符合要求的数据回来,但是,当抓到用户关键信息的时候,网易返回给我的是类似下面的一段字符串,我想着给他转成一个字典或者 python 内部能识别的数据格式,但是我发现我真的不知道怎么转化。我目前的方案之后一个,那就是正则匹配,但是我感觉正则匹配是不是蠢了点。有没有大神帮我解决下这个问题的。

    这儿是返回的字符串:(我已经做好了换行,记住是一个字符串,不是 python 的 list、tuple、dict)

    '([
            "farm_level":"3",
            "usernum":10836654,
            "iMaxExpt3":20,
            "iBeastSki4":17,
            "rent":88332,
            "iCGTotalAmount":30,
            "xianyu":12,
            "iHp_Max":2711,
            "more_attr":(["attrs":({(["lv":0,"idx":1,]),(["lv":0,"idx":3,]),(["lv":0,"idx":9,]),(["lv":0,"idx":12,]),(["lv":0,"idx":8,]),(["lv":0,"idx":6,]),(["lv":0,"idx":11,]),(["lv":0,"idx":7,]),(["lv":0,"idx":5,]),(["lv":0,"idx":4,]),(["lv":0,"idx":2,]),(["lv":0,"idx":10,]),(["lv":129,"idx":13,]),(["lv":444,"idx":14,]),}),]),
    
                "AllRider":([7:(["mattrib":"","ExtraGrow":0,"exgrow":12904,"iGrade":125,"iType":508,"all_skills":([]),]),5:(["mattrib":"","ExtraGrow":0,"exgrow":13106,"iGrade":125,"iType":503,"all_skills":([]),]),4:(["mattrib":"体质","ExtraGrow":0,"exgrow":13112,"iGrade":125,"iType":502,"all_skills":([611:1,]),]),6:(["mattrib":"魔力","ExtraGrow":100,"exgrow":23075,"iGrade":125,"iType":508,"all_skills":([600:2,601:3,]),]),2:(["mattrib":"力量","ExtraGrow":90,"exgrow":22297,"iGrade":125,"iType":503,"all_skills":([600:2,601:3,]),]),]),
    
            "iLearnCash":8213515,
            "iBadness":0,
            "ExAvt":([11:(["order":7,"cName":"锦绣幻梦","iType":12823,]),9:(["order":11,"cName":"藤蔓蔓延","iType":14694,]),6:(["order":4,"cName":"落星织","iType":12750,]),2:(["order":5,"cName":"蓝色妖姬","iType":14404,]),1:(["order":10,"cName":"星光熠熠","iType":21513,]),3:(["order":9,"cName":"锦绣幻梦","iType":12817,]),10:(["order":8,"cName":"两心相悦","iType":14654,]),8:(["order":6,"cName":"星光熠熠","iType":21514,]),7:(["order":1,"cName":"萌草发卡","iType":13696,]),39:(["order":2,"cName":"狼牙棒","iType":11169,]),40:(["order":12,"cName":"怀旧巨魔王炫卡","iType":19287,]),4:(["order":3,"cName":"落星织","iType":40013,]),]),
    
            "iDef_All":410,
            "iSewski":472,
            "iSumAmount":9,
            "iSpe_All":119,
            "iMaxExpt4":20,
            "sword_score":1000,
            "AchPointTotal":4520,
            "iGoodness":7230,
            "addPoint":28,
            "iSumAmountEx":1,
            "cName":"水儋澹兮生烟",
            "iSaving":0,
            "rent_level":5,
            "iBeastSki3":17,
    
            ......
    )]'
    
    12 条回复    2018-10-19 12:25:10 +08:00
    rabbbit
        1
    rabbbit  
       2018-10-18 17:38:05 +08:00
    import json
    json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
    gz911122
        2
    gz911122  
       2018-10-18 17:52:42 +08:00
    这不就是这个正常的 json 吗
    KomeijiSatori
        3
    KomeijiSatori  
       2018-10-18 17:56:20 +08:00
    cassidyhere
        4
    cassidyhere  
       2018-10-18 18:15:47 +08:00
    []换成{}, {}换成[],删除(),就是 dict 了
    frostming
        5
    frostming  
       2018-10-18 18:34:00 +08:00
    ()删掉
    {}和[]互换
    然后 json.loads
    zongren
        6
    zongren  
       2018-10-19 08:36:03 +08:00
    此贴终结
    moxiaowei
        7
    moxiaowei  
    OP
       2018-10-19 08:54:56 +08:00
    @gz911122 这是这正常的 json ?
    moxiaowei
        8
    moxiaowei  
    OP
       2018-10-19 08:55:14 +08:00
    @rabbbit 这样能用 我又何必来这儿发帖求助
    moxiaowei
        9
    moxiaowei  
    OP
       2018-10-19 08:55:34 +08:00
    @cassidyhere 关键就是如何替换
    aniua
        10
    aniua  
       2018-10-19 09:49:12 +08:00
    @moxiaowei 替换不就 replace 么
    chenstack
        11
    chenstack  
       2018-10-19 11:23:14 +08:00
    楼上说的差不多了,只是替换后仍不是标准 json,有的键为数字,且有逗号结尾,不能直接 json.loads。还有一个是你给的字符串开头是([,结尾是)],并不匹配,如果是成对的]),可以这样:
    eval(text.replace('([', '{').replace('])', '}').replace('({', '[').replace('})', ']'))
    如果想把()替换删掉,{}和[]互换,那替换时得加个临时的替换符号
    moxiaowei
        12
    moxiaowei  
    OP
       2018-10-19 12:25:10 +08:00
    哎 果然只有死方法,用正则先替换,谢谢大家 解析出来了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5772 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 06:34 · PVG 14:34 · LAX 22:34 · JFK 01:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.