繁体   English   中英

如何在 Python 中从用户那里获取 Unicode 输入?

[英]How to get Unicode input from user in Python?

这是代码: varUnicode = input('\\tEnter your Unicode\\n\\t>') print('\\u{}\u0026#39;.format(varUnicode))我想从用户那里获取 unicode 输入并打印字符。 在上面的代码中,python 给了我一个错误。

\\u\u003c/code>是在字符串文字中识别的转义序列:

仅在字符串文字中识别的转义序列是:

 Escape Meaning Notes Sequence \\N{name} Character named name in the Unicode database (4) \\uxxxx Character with 16-bit hex value xxxx (5) \\Uxxxxxxxx Character with 32-bit hex value xxxxxxxx (6)

笔记:

  1. 在 3.3 版更改: 添加了对名称别名 1 的支持。
  2. 正好需要四个十六进制数字。
  3. 任何 Unicode 字符都可以通过这种方式进行编码。 正好需要八个十六进制数字。

varUnicode = input('\tEnter your Unicode\n\t>')
print('\\u{}'.format(varUnicode.zfill(4)).encode('raw_unicode_escape').decode('unicode_escape'))

或(也许更好)

varUnicode = input('\tEnter your Unicode\n\t>')
print('\\U{}'.format(varUnicode.zfill(8)).encode('raw_unicode_escape').decode('unicode_escape'))

您需要在字符串前添加u以将其视为 Unicode。 print(u'\Р')将给出P

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM