【问题标题】:How to replace encode('string_escape') when migrating code from py2 to py3将代码从py2迁移到py3时如何替换encode('string_escape')
【发布时间】:2022-01-07 04:12:13
【问题描述】:

'string_escape' 已从 py3 中删除,我在将代码从 py2 迁移到 py3 时找不到替换 encode('string_escape') 的方法。有人可以帮我吗?

我在py2中的代码:

a = '["\\\"\\\""]'
print(a.encode('string_escape'))

预期输出:

["\\"\\""]

py3应该做什么?

【问题讨论】:

标签: python python-3.x


【解决方案1】:

感谢@Jan Wilamowski。 找到答案了。

>>> a = '["\\\"\\\""]'
>>> print(a.encode('unicode_escape').decode('utf-8'))
["\\"\\""]

【讨论】:

    猜你喜欢
    • 2020-01-19
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2019-06-17
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多