【问题标题】:Unescape string Python for MySQL为 MySQL 转义字符串 Python
【发布时间】:2017-07-07 13:04:28
【问题描述】:

我使用from MySQLdb import escape_string 来转义一个字符串。 如何取消转义字符串以取回原始字符串?

【问题讨论】:

    标签: python-2.7 mysql-python


    【解决方案1】:

    "your string".decode("string_escape")

    >>> escaped_string = "Hello\\nThere"
    >>>
    >>> unescaped_string = escaped_string.decode("string_escape")
    >>>
    >>> escaped_string
    'Hello\\nThere'
    >>>
    >>> unescaped_string
    'Hello\nThere'
    

    【讨论】:

    • @AminahNuraini 你在使用python 2.7吗?
    【解决方案2】:

    我是这样做的

    import ast
    
    a = 'Hello\\nThere'
    print(ast.literal_eval('"""{}"""'.format(a)))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      相关资源
      最近更新 更多