zoukankan      html  css  js  c++  java
  • Python 字符串转换为字典(String to Dict)

    一、需求

    为了处理从redis中拿到的value,如下

    {"appId":"ct","crawlSts":false,"health":"0","heartTime":"2018-12-10 00:23:57","localeIp":"129.204.161.75","loginNo":"13061634629","merchantId":"CMct18113000040"}

    需要把这个value处理为dict,但是在使用eval/literal_eval时,总是报错

    eval/literal_eval无法转换其中的   false

    二、解决方法

    python无法处理null、false、ture这样的字符串。

    比如:python中的变量“空”,不是null,也不是NULL,而是None,所以报错

    因此,我们需要经null、false、ture转换为其他字符

    global false

    false=''

     

    三、字符串转换为字典

    在工作中遇到一个小问题,需要将一个 python 的字符串转为字典,比如字符串:

    user_info = '{"name" : "john", "gender" : "male", "age": 28}'

    我们想把它转为下面的字典:

    user_dict = {"name" : "john", "gender" : "male", "age": 28}

    有以下几种方法:

    1、通过 json 来转换

    >>> import json
    >>> user_info= '{"name" : "john", "gender" : "male", "age": 28}'
    >>> user_dict = json.loads(user_info)
    >>> user_dict
    {u'gender': u'male', u'age': 28, u'name': u'john'}

    但是使用 json 进行转换存在一个潜在的问题。

    由于 json 语法规定 数组或对象之中的字符串必须使用双引号,不能使用单引号 (官网上有一段描述是 “A string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes” ),因此下面的转换是错误的:

    复制代码
    >>> import json
    >>> user_info = "{'name' : 'john', 'gender' : 'male', 'age': 28}"
    # 由于字符串使用单引号,会导致运行出错
    >>> user_dict = json.loads(user_info)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 339, in loads
        return _default_decoder.decode(s)
      File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 364, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 380, in raw_decode
        obj, end = self.scan_once(s, idx)
    ValueError: Expecting property name: line 1 column 2 (char 1)
    复制代码

    2、通过 eval

    复制代码
    >>> user_info = '{"name" : "john", "gender" : "male", "age": 28}'
    >>> user_dict = eval(user_info)
    >>> user_dict
    {'gender': 'male', 'age': 28, 'name': 'john'}
    >>> user_info = "{'name' : 'john', 'gender' : 'male', 'age': 28}"
    >>> user_dict = eval(user_info)
    >>> user_dict
    {'gender': 'male', 'age': 28, 'name': 'john'}
    复制代码

    通过 eval 进行转换就不存在上面使用 json 进行转换的问题。但是,使用 eval 却存在安全性的问题,比如下面的例子:

    复制代码
    # 让用户输入 `user_info`
    >>> user_info = raw_input('input user info: ')
    # 输入 {"name" : "john", "gender" : "male", "age": 28},没问题
    >>> user_dict = eval(user_info)
    # 输入 __import__('os').system('dir'),user_dict 会列出当前的目录文件!
    # 再输入一些删除命令,则可以把整个目录清空了!
    >>> user_dict = eval(user_info)
    复制代码

    3、通过 literal_eval

    复制代码
    >>> import ast
    >>> user = '{"name" : "john", "gender" : "male", "age": 28}'
    >>> user_dict = ast.literal_eval(user)
    >>> user_dict
    {'gender': 'male', 'age': 28, 'name': 'john'}
    user_info = "{'name' : 'john', 'gender' : 'male', 'age': 28}"
    >>> user_dict = ast.literal_eval(user)
    >>> user_dict
    {'gender': 'male', 'age': 28, 'name': 'john'}
    复制代码

    使用 ast.literal_eval 进行转换既不存在使用 json 进行转换的问题,也不存在使用 eval 进行转换的 安全性问题,因此推荐使用 ast.literal_eval

    整理自:

    http://funhacks.net/2016/04/24/python_将字符串转为字典/

    https://www.cnblogs.com/zhuyue1/p/6427763.html

  • 相关阅读:
    为什么不要用VSCODE来写Makefile
    JavaFX第三弹
    javaFX文件和文件夹选择器
    写了一个vsftpd的GUI
    在java中调用shell命令和执行shell脚本
    正交投影与斯密特正交化的好处
    Linux下安装软件
    C++中的仿函数
    C++中重载操作符[ ]
    使用斐波那契查找
  • 原文地址:https://www.cnblogs.com/xibuhaohao/p/10113226.html
Copyright © 2011-2022 走看看