[英]Python print string with unicode escape characters
I'm trying to print the literal unicode escape character of "Pedro Le\ón".我正在尝试打印“Pedro Le\ón”的文字 unicode 转义字符。 When I do the following:当我执行以下操作时:
test = "Pedro Le\u00F3n"
print(test)
>>> Pedro León
How can I get it to output "Pedro Le\ón"?我怎样才能让它输出“Pedro Le\ón”?
Encode to bytes with the unicode_escape
encoding , and decode right back:使用unicode_escape
encoding编码为字节,然后立即解码:
>>> out = test.encode('unicode_escape').decode()
>>> out
'Pedro Le\\xf3n'
>>> print(out)
Pedro Le\xf3n
Note that it's a \\xXX
escape instead of a \\uXXXX
escape, since it's less than U+FF.请注意,它是\\xXX
转义而不是\\uXXXX
转义,因为它小于 U+FF。 For comparison:比较:
>>> '\u0080'
'\x80'
You need to use raw strings.您需要使用原始字符串。 Simply use r
before your string:只需在字符串前使用r
:
test = r"Pedro Le\\u00F3n"
print(test)
Output: Pedro Le\\\ón
输出: Pedro Le\\\ón
try this试试这个
test = "Pedro Le\\u00F3n"
print(test)
it causes because in python there are many "special characters like" '\\n' and more and if you want to ignore it you need to write \\ \\ instead of \\这是因为在 python 中有许多“特殊字符,如” '\\n' 等等,如果你想忽略它,你需要写 \\\\ 而不是 \\
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.