我有以下字符串 "◣⛭◣◃✺▲♢"
我想将该字符串变成 "\u25E3\u26ED\u25E3\u25C3\u273A\u25B2\u2662 “
。和本站一模一样https://mothereff.in/js-escapes
我想知道这在 python 中是否可行。我已经尝试从 unicode 文档中为 python 分配一些东西,但失败得很惨。
我之前尝试过的例子:
#!/usr/bin/env python
# -*- coding: latin-1 -*-
f = open('js.js', 'r').read()
print(ord(f[:1]))
帮助将不胜感激!
最佳答案
考虑到您使用的是 Python 3:
unicode_string="◣⛭◣◃✺▲♢"
byte_string= unicode_string.encode('ascii', 'backslashreplace')
print(byte_string)
参见 codecs更多信息的模块文档。
然而,为了使用 JavaScript 符号,有一个特殊的模块 json , 然后你可以达到同样的目的:
import json
unicode_string="◣⛭◣◃✺▲♢"
json_string=json.dumps(unicode_string)
print(json_string)
关于javascript - 如何在 python 中转义 UNICODE 字符串(到 javascript 转义),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35383275/