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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (81)
[{"answerInfo":{"extraData":{"am_answer_type":"NN"}},"content":"MP3:not support.","messageId":"c4d6a2f4649d483a811fcce4b26ae9a1"}]

如何使用正则表达式或python代码从此String中提取“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)
提问于
用户回答回答于

我建议使用json,而不是regex

https://onlinegdb.com/r1SKCe987

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"])
用户回答回答于
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.'

扫码关注云+社区

领取腾讯云代金券