簡體   English   中英

如何在Python中打印Unicode字符?

[英]How to print Unicode character in Python?

我想制作一本字典,其中英語單詞指向俄語和法語翻譯。

如何在 Python 中打印出 unicode 個字符? 另外,如何在變量中存儲 unicode 個字符?

要在 Python 源代碼中包含 Unicode 字符,您可以在字符串中使用形式的Unicode 轉義字符 在 Python 2.x 中,您還需要在字符串文字前加上 'u'。

這是在 Python 2.x 交互式控制台中運行的示例:

>>> print u'\u0420\u043e\u0441\u0441\u0438\u044f'
Россия

在 Python 2 中,在字符串前加上 'u' 將它們聲明為 Unicode 類型變量,如Python Unicode 文檔 中所述

在 Python 3 中,“u”前綴現在是可選的:

>>> print('\u0420\u043e\u0441\u0441\u0438\u044f')
Россия

如果運行上述命令沒有為您正確顯示文本,則可能您的終端無法顯示 Unicode 字符。

這些示例使用 Unicode 轉義符 ( \\u...\u003c/code> ),它允許您打印 Unicode 字符,同時將源代碼保持為純 ASCII。 在不同系統上使用相同的源代碼時,這會有所幫助。 如果您確信所有系統都能正確處理 Unicode 文件,您還可以直接在 Python 源代碼中使用 Unicode 字符(例如,在 Python 2 中print u'Россия' )。

有關從文件中讀取 Unicode 數據的信息,請參閱此答案:

用 Python 從文件中讀取字符

在 Python 中打印一個 unicode 字符:

直接從 python 解釋器打印一個 unicode 字符:

el@apollo:~$ python
Python 2.7.3
>>> print u'\u2713'
✓

Unicode 字符u'\✓'是一個復選標記。 解釋器在屏幕上打印復選標記。

從 python 腳本打印一個 unicode 字符:

把它放在 test.py 中:

#!/usr/bin/python
print("here is your checkmark: " + u'\u2713');

像這樣運行它:

el@apollo:~$ python test.py
here is your checkmark: ✓

如果它沒有為您顯示復選標記,則問題可能出在其他地方,例如終端設置或您正在使用流重定向進行的某些操作。

將 unicode 字符存儲在文件中:

將此保存到文件:foo.py:

#!/usr/bin/python -tt
# -*- coding: utf-8 -*-
import codecs
import sys 
UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)
print(u'e with obfuscation: é')

運行它並將輸出通過管道傳輸到文件:

python foo.py > tmp.txt

打開 tmp.txt 並查看內部,您會看到:

el@apollo:~$ cat tmp.txt 
e with obfuscation: é

因此,您已將帶有混淆標記的 unicode e 保存到文件中。

如果您嘗試print() Unicode,並收到 ascii 編解碼器錯誤,請查看此頁面,其 TLDR 在啟動 python 之前執行export PYTHONIOENCODING=UTF-8 (此變量控制控制台嘗試的字節序列將您的字符串數據編碼為)。 在內部,Python3 默認使用 UTF-8(參見Unicode HOWTO ),所以這不是問題; 您可以將 Unicode 放入字符串中,如其他答案和評論中所示。 當您嘗試將這些數據輸出到您的控制台時,問題就會發生。 Python 認為您的控制台只能處理 ascii。 其他一些答案說,“首先將其寫入文件”但請注意,他們為此指定了編碼(UTF-8)(因此,Python 不會更改任何寫入內容),然后使用一種方法進行讀取只吐出字節而不考慮編碼的文件,這就是為什么它有效。

在 Python 2 中,您使用u聲明 unicode 字符串,如u"貓"並使用decode()encode()分別轉換為 unicode 和從 unicode 轉換。

它在 Python 3 中要容易得多。可以在這里找到一個很好的概述。 那場演講為我澄清了很多事情。

考慮到這是 google 搜索此主題時的第一個堆棧溢出結果,值得一提的是,在 Python 3 中將u加到 unicode 字符串是可選的。(Python 2 示例是從頂部答案中復制的)

Python 3(都工作):

print('\u0420\u043e\u0441\u0441\u0438\u044f')
print(u'\u0420\u043e\u0441\u0441\u0438\u044f')

蟒蛇2:

print u'\u0420\u043e\u0441\u0441\u0438\u044f'

'+'替換為'000' 例如, 'U+1F600'將變為'U0001F600'並在 Unicode 代碼前加上“\\”並打印。 例子:

>>> print("Learning : ", "\U0001F40D")
Learning :  🐍
>>> 

檢查這個也許它會幫助python unicode emoji

我在 Windows 中使用 Portable winpython,它包括 IPython QT 控制台,我可以實現以下功能。

>>>print ("結婚")
結婚

>>>print ("おはよう")
おはよう

>>>str = "結婚"


>>>print (str)
結婚

您的控制台解釋器應該支持 unicode 以顯示 unicode 字符。

還有一件事尚未添加

在 Python 2 中,如果要打印具有 unicode 的變量並使用.format() ,請執行此操作(使用u''將正在格式化的基本字符串.format() unicode 字符串:

>>> text = "Université de Montréal"
>>> print(u"This is unicode: {}".format(text))
>>> This is unicode: Université de Montréal

Python 支持\N作為命名的 unicode 字符,如果您想讓代碼更具可讀性,這會很方便。 這是一個例子:

assert '\N{snake}' == '🐍'

這修復了 python 中的 UTF-8 打印:

UTF8Writer = codecs.getwriter('utf8')
sys.stdout = UTF8Writer(sys.stdout)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM