字符串转化问题

2018-10-18 17:22:17 +08:00
 moxiaowei

比较喜欢玩梦幻西游,最近准备买号,自己看梦幻藏宝阁呢 又没空,就想着用 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,

        ......
)]'
1830 次点击
所在节点    Python
12 条回复
rabbbit
2018-10-18 17:38:05 +08:00
import json
json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
gz911122
2018-10-18 17:52:42 +08:00
这不就是这个正常的 json 吗
KomeijiSatori
2018-10-18 17:56:20 +08:00
cassidyhere
2018-10-18 18:15:47 +08:00
[]换成{}, {}换成[],删除(),就是 dict 了
frostming
2018-10-18 18:34:00 +08:00
()删掉
{}和[]互换
然后 json.loads
zongren
2018-10-19 08:36:03 +08:00
此贴终结
moxiaowei
2018-10-19 08:54:56 +08:00
@gz911122 这是这正常的 json ?
moxiaowei
2018-10-19 08:55:14 +08:00
@rabbbit 这样能用 我又何必来这儿发帖求助
moxiaowei
2018-10-19 08:55:34 +08:00
@cassidyhere 关键就是如何替换
aniua
2018-10-19 09:49:12 +08:00
@moxiaowei 替换不就 replace 么
chenstack
2018-10-19 11:23:14 +08:00
楼上说的差不多了,只是替换后仍不是标准 json,有的键为数字,且有逗号结尾,不能直接 json.loads。还有一个是你给的字符串开头是([,结尾是)],并不匹配,如果是成对的]),可以这样:
eval(text.replace('([', '{').replace('])', '}').replace('({', '[').replace('})', ']'))
如果想把()替换删掉,{}和[]互换,那替换时得加个临时的替换符号
moxiaowei
2018-10-19 12:25:10 +08:00
哎 果然只有死方法,用正则先替换,谢谢大家 解析出来了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/498860

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX