如何从 Python 中的用户获取 Unicode 输入?
How to get Unicode input from user in Python?
这是代码:
varUnicode = input('\tEnter your Unicode\n\t>') print('\u{}'.format(varUnicode))
我想从用户那里获取 unicode 输入并打印字符。在上面的代码中 python 给我一个错误。
您需要在字符串前添加一个u
以将其视为Unicode。 print(u'\u0420')
会给出 P
.
\u
是在 string literals:
中识别的转义序列
Escape sequences only recognized in string literals are:
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)
Notes:
- Changed in version 3.3: Support for name aliases 1 has been added.
- Exactly four hex digits are required.
- Any Unicode character can be encoded this way. Exactly eight hex digits are required.
使用
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'))
这是代码:
varUnicode = input('\tEnter your Unicode\n\t>') print('\u{}'.format(varUnicode))
我想从用户那里获取 unicode 输入并打印字符。在上面的代码中 python 给我一个错误。
您需要在字符串前添加一个u
以将其视为Unicode。 print(u'\u0420')
会给出 P
.
\u
是在 string literals:
Escape sequences only recognized in string literals are:
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)
Notes:
- Changed in version 3.3: Support for name aliases 1 has been added.
- Exactly four hex digits are required.
- Any Unicode character can be encoded this way. Exactly eight hex digits are required.
使用
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'))