这是转换为表情符号的后续行动.在那个问题中,OP有一个json.dumps()带有表情符号代表对的表情符号的编码文件 - \ud83d\ude4f.他/她在阅读文件和正确翻译表情符号时遇到问题,正确的答案是json.loads()文件中的每一行,json模块将处理从代理对转换回(我假设是UTF8编码的)表情符号.
所以这是我的情况:说我只有一个常规的Python 3 unicode字符串,其中包含一个代理项:
emoji = "This is \ud83d\ude4f, an emoji."
Run Code Online (Sandbox Code Playgroud)
如何处理此字符串以获取表情符号的表示?我希望得到这样的东西:
"This is , an emoji."
# or
"This is \U0001f64f, an emoji."
Run Code Online (Sandbox Code Playgroud)
我试过了:
print(emoji)
print(emoji.encode("utf-8")) # also tried "ascii", "utf-16", and "utf-16-le"
json.loads(emoji) # and `.encode()` with various codecs
Run Code Online (Sandbox Code Playgroud)
一般来说,我得到一个类似的错误UnicodeEncodeError: XXX codec can't encode character '\ud83d' in position 8: surrogates no allowed.
我在Linux上运行Python 3.5.1,$LANG设置为en_US.UTF-8.我在命令行的Python解释器和Sublime Text中运行的IPython中运行这些示例 - 似乎没有任何差异.
这段代码:
for root, dirs, files in os.walk('.'):
print(root)
Run Code Online (Sandbox Code Playgroud)
给我这个错误:
UnicodeEncodeError: 'utf-8' codec can't encode character '\udcc3' in position 27: surrogates not allowed
Run Code Online (Sandbox Code Playgroud)
如何在没有像这样的有毒字符串的情况下浏览文件树?