[{"answerInfo":{"extraData":{"am_answer_type":"NN"}},"content":"MP3:not support.","messageId":"c4d6a2f4649d483a811fcce4b26ae9a1"}]
如何使用正则表达式或python代码从此String中提取“ MP3:不支持”?
但是根据建议生成了一个错误:
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)
答案 0 :(得分:1)
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 :(得分:1)
根据您的样本,我建议使用json
而不是regex
。
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"])