如何像“;u{variable}”;在Python2.7中?

如何像“;u{variable}”;在Python2.7中?,python,unicode,python-unicode,Python,Unicode,Python Unicode,例如,我可以打印Unicode符号,如: print u'\u00E0' 或 但看起来我不能这样做: a = '\u00E0' print someFunctionToDisplayTheCharacterRepresentedByThisCodePoint(a) 主要用例将在循环中。我有一个unicode代码点列表,我希望在控制台上显示它们。比如: with open("someFileWithAListOfUnicodeCodePoints") as uniCodeFile: f

例如,我可以打印Unicode符号,如:

print u'\u00E0'

但看起来我不能这样做:

a = '\u00E0'
print someFunctionToDisplayTheCharacterRepresentedByThisCodePoint(a)
主要用例将在循环中。我有一个unicode代码点列表,我希望在控制台上显示它们。比如:

with open("someFileWithAListOfUnicodeCodePoints") as uniCodeFile:
    for codePoint in uniCodeFile:
        print codePoint #I want the console to display the unicode character here
该文件有一个unicode代码点列表。例如:

2109
OOBO
00E4
1F1E6
循环应输出:

℉
°
ä
This is probably not a great way, but it's a start:

>>> x = '00e4'
>>> print unicode(struct.pack("!I", int(x, 16)), 'utf_32_be')
ä
℉
°
ä

这可能不是一个好方法,但这是一个开始:

int2bytes = struct.Struct("!I").pack
with open("someFileWithAListOfUnicodeCodePoints") as fh:
    for code_point in fh:
        print unicode(int2bytes(int(code_point, 16)), 'utf_32_be')
首先,我们得到十六进制字符串
x
表示的整数。我们将其打包成一个字节字符串,然后使用
utf\u32\ube
编码对其进行解码

由于您经常这样做,因此可以预编译结构:

>>> print int2bytes(int('00e4', 16)).decode('utf_32_be')
ä
如果您认为更清楚,也可以直接使用
decode
方法而不是
unicode
类型:

>>> str(int('00e4', 16).to_bytes(4, 'big'), 'utf_32_be')
"ä"
Python 3在
int
类中添加了一个
to_bytes
方法,该方法允许您绕过
struct
模块:

with open("someFileWithAListOfUnicodeCodePoints", "rb") as uniCodeFile:
    for codePoint in uniCodeFile:
        print "\\u" + codePoint.strip()).decode("unicode-escape")

这可能不是一个好方法,但这是一个开始:

int2bytes = struct.Struct("!I").pack
with open("someFileWithAListOfUnicodeCodePoints") as fh:
    for code_point in fh:
        print unicode(int2bytes(int(code_point, 16)), 'utf_32_be')
首先,我们得到十六进制字符串
x
表示的整数。我们将其打包成一个字节字符串,然后使用
utf\u32\ube
编码对其进行解码

由于您经常这样做,因此可以预编译结构:

>>> print int2bytes(int('00e4', 16)).decode('utf_32_be')
ä
如果您认为更清楚,也可以直接使用
decode
方法而不是
unicode
类型:

>>> str(int('00e4', 16).to_bytes(4, 'big'), 'utf_32_be')
"ä"
Python 3在
int
类中添加了一个
to_bytes
方法,该方法允许您绕过
struct
模块:

with open("someFileWithAListOfUnicodeCodePoints", "rb") as uniCodeFile:
    for codePoint in uniCodeFile:
        print "\\u" + codePoint.strip()).decode("unicode-escape")

这些是unicode代码点,但缺少python unicode转义。所以,把它放进去:

这在给定系统上是否有效取决于控制台的编码。如果它是一个Windows代码页,并且字符不在它的范围内,你仍然会得到令人讨厌的错误


在Python3中,这将是
b“\\u”

这些是unicode代码点,但缺少
\u
PythonUnicode转义。所以,把它放进去:

这在给定系统上是否有效取决于控制台的编码。如果它是一个Windows代码页,并且字符不在它的范围内,你仍然会得到令人讨厌的错误


在python 3中,这将是
b“\\u”

您想要
打印unichr(int('00E0',16))
。将十六进制字符串转换为整数并打印其Unicode码点

警告:在Windows上,codepoints>U+FFFF将不起作用

解决方案:使用Python3.3+
print(chr(int(line,16)))


在所有情况下,您仍然需要使用支持代码点标志符号的字体。

您希望
打印unichr(int('00E0',16))
。将十六进制字符串转换为整数并打印其Unicode码点

警告:在Windows上,codepoints>U+FFFF将不起作用

解决方案:使用Python3.3+
print(chr(int(line,16)))


在所有情况下,您仍然需要使用支持代码点标志符号的字体。

能否提供所需输出的确切示例?不清楚您想要什么,但可能是
unicodedata.name(a)
。您不能将
a=u'\u00E0'
传递给函数吗?我不确定您想要什么,但如果您试图打印unicode文本文件的内容,我认为您当前的代码应该可以工作。@chepner我在问题中添加了示例输入和输出。@Colin不,我不能这样做,因为我没有字符串文字“00E0”。此文字位于变量中。例如a='00E0'。如果是字符串文字,我可以像u'\u00E0'那样执行。但是因为我有一个变量,我需要做一些类似于a=u+“\u”+codePointVariable的事情。这不是一个有效的调用,因为unicode指示符“u”在语句中的工作方式与此不同。能否提供所需输出的确切示例?不清楚您想要什么,但可能是
unicodedata.name(a)
。您不能将
a=u'\u00E0'
传递给函数吗?我不确定您想要什么,但如果您试图打印unicode文本文件的内容,我认为您当前的代码应该可以工作。@chepner我在问题中添加了示例输入和输出。@Colin不,我不能这样做,因为我没有字符串文字“00E0”。此文字位于变量中。例如a='00E0'。如果是字符串文字,我可以像u'\u00E0'那样执行。但是因为我有一个变量,我需要做一些类似于a=u+“\u”+codePointVariable的事情。这不是一个有效的调用,因为unicode指示符“u”在语句中不是这样工作的。是的,这很有效!但我也希望有人能用一种更简单的方式发布。这对我来说太复杂了。谢谢!我现在就用这个。是的,这个有用!但我也希望有人能用一种更简单的方式发布。这对我来说太复杂了。谢谢!我现在就用这个。