【发布时间】:2018-08-30 20:44:09
【问题描述】:
我正在从网页的 html 中提取格式为
的列表lst = '["a","b","c"]' # (type <str>)
上面的数据类型是 str 我希望它转换成 python list type ,像这样
lst = ["a","b","c"] #(type <list>)
我可以通过
lst = lst[1:-1].replace('"','').split(',')
但由于a,b &c的实际值相当长且复杂(包含长html文本),我不能依赖上述方法。
我也尝试使用 json 模块并使用 json.loads(lst) ,这给出了以下异常
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python2.7/json/decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
在 Python 中转换为列表的任何方式?
编辑:列表的实际值为:
['reqlistitem.no','reqlistitem.applyonlinejobdesc','reqlistitem.no','reqlistitem.referjobdesc','reqlistitem.applyemailsubjectapplication','reqlistitem.applyemailjobdesc','reqlistitem.no','reqlistitem.addedtojobcart','reqlistitem.displayjobcartactionjobdesc','reqlistitem.shareURL','reqlistitem.title','reqlistitem.shareable','reqlistitem.title','reqlistitem.contestnumber','reqlistitem.contestnumber','reqlistitem.description','reqlistitem.description','reqlistitem.primarylocation','reqlistitem.primarylocation','reqlistitem.otherlocations','reqlistitem.jobschedule','reqlistitem.jobschedule','reqlistitem.jobfield','reqlistitem.jobfield','reqlistitem.displayreferfriendaction','reqlistitem.no','reqlistitem.no','reqlistitem.applyonlinejobdesc','reqlistitem.no','reqlistitem.referjobdesc','reqlistitem.applyemailsubjectapplication','reqlistitem.applyemailjobdesc','reqlistitem.no','reqlistitem.addedtojobcart','reqlistitem.displayjobcartactionjobdesc','reqlistitem.shareURL','reqlistitem.title','reqlistitem.shareable']
【问题讨论】:
-
您需要显示您正在使用的实际代码和数据。
json.loads(lst)工作正常,如果lst如您在上面显示的那样。 -
我已经编辑了实际值