首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从该字符串格式中提取该字段?

如何从该字符串格式中提取该字段?
EN

Stack Overflow用户
提问于 2018-08-22 02:04:38
回答 2查看 43关注 0票数 -2
[{"answerInfo":{"extraData":{"am_answer_type":"NN"}},"content":"MP3:not support.","messageId":"c4d6a2f4649d483a811fcce4b26ae9a1"}]

如何使用正则表达式或python代码从该字符串中提取"MP3: not support“?

但根据建议生成了一个错误:

Traceback (most recent call last):
  File "/Users/congminmin/PycharmProjects/Misc/csv/csvLoader.py", line 16, in <module>
    print(question+ " " + json.loads(answer)[0]['content'])
  File "/Users/congminmin/anaconda3/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/Users/congminmin/anaconda3/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/congminmin/anaconda3/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
EN

回答 2

Stack Overflow用户

发布于 2018-08-22 02:09:34

Python 3.6.5 (default, Jun 17 2018, 12:13:06)
>>> text = '[{"answerInfo":{"extraData":{"am_answer_type":"NN"}},"content":"MP3:not support.","messageId":"c4d6a2f4649d483a811fcce4b26ae9a1"}]'
>>> import json
>>> json.loads(text)[0]['content']
'MP3:not support.'
票数 1
EN

Stack Overflow用户

发布于 2018-08-22 02:11:24

根据您的示例,我建议使用json而不是regex

Check it running here

import json

json_data = '[{"answerInfo":{"extraData":{"am_answer_type":"NN"}},"content":"MP3:not support.","messageId":"c4d6a2f4649d483a811fcce4b26ae9a1"}]'

python_obj = json.loads(json_data)
print(python_obj[0]["content"])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51954369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档