美文网首页程序员
把一个字符串类型的列表或字典转成列表和字典

把一个字符串类型的列表或字典转成列表和字典

作者: _AlphaBaby_ | 来源:发表于2018-09-30 08:41 被阅读0次
    >>> s = "[1,2,3,4,5,6,7]"
    >>> import json
    >>> json.loads(s)
    [1, 2, 3, 4, 5, 6, 7]
    
    >>> import json
    >>> s = '{"a":"b","x":3}'
    >>> json.loads(s)
    {'a': 'b', 'x': 3}
    

    注意在转化字典的时候字典的里面必需都是双引号,否则报错如

    >>> s = '{"a":"b",\'x\':3}'
    >>> import json
    >>> json.loads(s)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 354, in loads
        return _default_decoder.decode(s)
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 339, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/decoder.py", line 355, in raw_decode
        obj, end = self.scan_once(s, idx)
    json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 10 (char 9)
    

    相关文章

      网友评论

        本文标题:把一个字符串类型的列表或字典转成列表和字典

        本文链接:https://www.haomeiwen.com/subject/dzmboftx.html