當前位置:首頁 » 編程語言 » pythonasciitoutf8

pythonasciitoutf8

發布時間: 2024-09-22 23:15:50

python String和PyQt QString的區別

以下在python2.5和PyQt4.4.6 for python2.5環境下討論。

在python中有兩種與字元有關的類型:string object和Unicode object。

平時進行輸入輸出的一般都用string
object,當需要顯示一些特殊字元或者中文等文字時候,需要轉換為Unicode編碼。在PyQt中也有兩種字元類型與上面兩者對應:QByteArray和QString,主要是使用QString操作數據。

1) python string
object可以理解為一個接一個位元組的位元組組,至於表示什麼編碼,與表示文字有關,比如「python
string」,「中文」。注意它是有不同編碼區分的。

PyQt中與之對應的是QbyteArray,而不是Qstring。

A built-in string object (plain or Unicode) is a sequence of
characters used to store and represent text-based information
(plain strings are also sometimes used to store and represent
arbitrary sequences of binary bytes). (摘自《Python in a
NutShell》)

QByteArray can be used to store both raw bytes (including '"0's)
and traditional 8-bit '"0'-terminated.(摘自《PyQt手冊》)

2)Python Unicode
object可以理解為固定使用utf-16編碼的位元組組,其中英文和中文都使用兩個位元組(16位)來表示,如:u"Python
Unicode object"、u"中文"。
PyQt中與之對應的就是QString了。

Unicode string literals have the same syntax as other string
literals, with a u or U immediately before the leading quote.
(摘自《Python in a NutShell》)

Qt also provides the QString class to store string data. It stores
16-bit Unicode characters, making it easy to store
non-ASCII/non-Latin-1 characters in your
application.(摘自《PyQt手冊》)
QString stores a string of 16-bit QChars, where each QChar
corresponds one Unicode 4.0 character.(摘自《PyQt手冊》)

2 PyQt內部類型轉換

QString有
toAscii()、toUtf8()函數轉換為QByteArray類型,(這個基本不用,因為很少直接用QByteArray類型)有__init__
(self, QByteArray a)函數將QByteArray類型轉為QString。

3. Python string object和Python Unicode object相互轉換

1)Python string object是原始編碼是有區分的,通過 decode('原始編碼')
函數解碼得到通用utf16編碼即Python Unicode object。
>>>"python
string".decode('ascii')
或者
>>>"python
string".decode()
得到 u"python string"
因為默認按ascii解碼。
>>>"中文".decode('gbk')
得到 u""u4e2d"u6587" ,列印出來就是 中文 二字。(注意結果是2位元組一組,共兩組,對應兩個漢字)
又:"python string".decode('gkb') ,即按漢字來解碼,也可以得到 u"python
string",因為gbk編碼也支持英文字母;
但是"中文".decode('ascii') 即按ascii解碼是錯誤的,因為ascii編碼不支持漢字!

>>>
"dfdf".decode()
u'dfdf'
>>>
"dfdf".decode("ascii")
u'dfdf'
>>>
"dfdf".decode("gbk")
u'dfdf'
>>>
"中文".decode("gbk")
u'"u4e2d"u6587'
>>>print
"中文".decode("gbk")
中文
>>>
"中文".decode("gb2312")
u'"u4e2d"u6587'
>>>
"中文".decode("ascii")
Traceback (most recent call last):
File "<interactive input>", line 1,
in <mole>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd6 in
position 0: ordinal not in range(128)

2)Python Unicode object原始編碼固定是utf16,通過 encode('目的編碼') 編碼來得到Python
string object。
>>>u"unicode
string".encode()
或者
>>>u"unicode
string".encode('ascii')
得到
'unicode string',默認目的編碼為ascii。
>>>u"中文".encode("gbk")
得到'"xd4"xd0"xce"xc4',列印出來就是 中文。(注意結果是1位元組一組,共4組)

>>>
u"sdff".encode()
'sdff'
>>>
u"sdff".encode('ascii')
'sdff'
>>>
u"sdff".encode('gbk')
'sdff'
>>>
u"sdff".encode('gb2312')
'sdff'
>>>
u"中文".encode('gbk')
'"xd6"xd0"xce"xc4'
>>> print
u"中文".encode('gbk')
中文
>>>
u"中文".encode('ascii')
Traceback (most recent call last):
File "<stdin>", line 1, in
<mole>
UnicodeEncodeError: 'ascii' codec can't encode characters in
position 0-1: ordin
al not in range(128)
注意:執行>>>
u"中文".encode('gbk')命令需要你的IDE支持gbk編碼,在官方shell下執行肯定沒問題,但如果你的IDE比如PyWin中文輸入異常,則可能報錯。

4. Python string object和Python Unicode object向QString的轉換。

Qt一般不直接操作QByteArray,只需關注Python string object和Python Unicode
object向QString的轉換。
很多關於PyQt4的英文書籍說:PyQt函數需要QString參數的地方都可以直接用Python string
object或者Python Unicode object,如果非要轉換可以直接用QtCore.QString()構造。比如《GUI
Programming with PyQt》,再如《PyQt手冊》:

Whenever PyQt expects a QString as a function argument, a Python
string object or a Python Unicode object can be provided instead,
and PyQt will do the necessary conversion automatically.

You may also manually convert Python string and Unicode objects to
QString instances by using the QString constructor as demonstrated
in the following code fragment:

qs1 = QtCore.QString("Converted Python string object")
qs2 = QtCore.QString(u"Converted Python Unicode object")

但可惜這只適用於英文即ascii編碼,對於中文則行不通!

直接的QString:
>>>
QtCore.QString('中文')
PyQt4.QtCore.QString(u'"xd6"xd0"xce"xc4')
>>> print
QtCore.QString('中文')
Traceback (most recent call last):
File "<stdin>", line 1, in
<mole>
UnicodeEncodeError: 'ascii' codec can't encode characters in
position 0-3: ordin
al not in range(128)
>>>
>>>
QtCore.QString(u'中文')
PyQt4.QtCore.QString(u'"u4e2d"u6587')
>>> print
QtCore.QString(u'中文')
Traceback (most recent call last):
File "<stdin>", line 1, in
<mole>
UnicodeEncodeError: 'ascii' codec can't encode characters in
position 0-1: ordin
al not in range(128)
>>>
因為它們都是默認按ascii編碼轉換!

GUI編程
可以創建一個QTextEdit對象myTextEdit, 檢驗:
myTextEdit.append("中文")
或者
myTextEdit.append(u"中文")
或者
myTextEdit.append(QtCore.QString('中文'))
或者
myTextEdit.append(QtCore.QString(u'中文'))
你會發現顯示都是亂碼...因為它們都是默認按ascii編碼進行內部轉換得到QString相應utf16編碼的。

解決方法是:
利用unicode()函數顯示指定gb2312編碼進行中文編碼轉換,轉換後的Python Unicode
object則是可以直接作為QString參數代入用的:

>>> unicode('中文',
'gb2312', 'ignore')
u'"u4e2d"u6587'
>>> print
unicode('中文', 'gb2312', 'ignore')
中文
>>>

myTextEdit.append(unicode('中文', 'gb2312', 'ignore'))
#用以替代myTextEdit.append(u"中文")
或者多此一舉下:
myTextEdit.append(QtCore.QString(unicode('中文', 'gb2312',
'ignore')))
#用以替代myTextEdit.append(QtCore.QString(u'中文'))

5. QString向Python string object和Python Unicode object的轉換。

Python中需要用Python string object和Python Unicode
object的地方可就不一定可以直接用QString了!!!
QString向Python string object轉換可以理解,因為編碼不同。
QString向Python Unicode object的轉換?需要轉換嗎?不都是utf16編碼嗎?
QString是tuf16編碼,但是它的實現並非Python Unicode
object那樣直接的utf16碼,而實際是一個QChar串,每個QChar才對應unicode符,所以地位相當但並不相同。
許多英文書籍寫到:可以使用str()函數直接將QString轉換為Python string
object,可以使用unicode()直接將QString轉換為Python Unicode
object。如《PyQt手冊》:

In order to convert a QString to a Python string object use the
Python str() builtin. Applying str() to a null QString and an empty
QString both result in an empty Python string object.

In order to convert a QString to a Python Unicode object use the
Python unicode() builtin. Applying unicode() to a null QString and
an empty QString both result in an empty Python Unicode
object.

但同樣只適用於英文,具體見下面分別分析。
1)QString向Python Unicode object的轉換。
>>> from PyQt4 import
QtGui, QtCore
>>>
unicode(QtCore.QString('def'))
u'def'
>>> print
unicode(QtCore.QString('def'))
def

對於中文,unicode()必須要指定編碼後有效。(這樣也只針對直接的QString有效?對於Qt
GUI編程中,從QWidget取得的QString無效?)

>>> from PyQt4 import
QtGui, QtCore
>>>
unicode(QtCore.QString('中文'))
u'"xd6"xd0"xce"xc4'
>>> print
unicode(QtCore.QString('中文'))
Traceback (most recent call last):
File "<stdin>", line 1, in
<mole>
UnicodeEncodeError: 'gbk' codec can't encode character u'"xd6' in
position 0: il
legal multibyte sequence

指定原始編碼後:
>>>
unicode(QtCore.QString('中文'),'gbk','ignore')
u'"u4e2d"u6587'
>>> print
unicode(QtCore.QString('中文'),'gbk','ignore')
中文 TEST

Ⅱ 有哪些炫酷的代碼編輯器

題主所說的炫酷的代碼編輯器,也許更多的只是主題的絢麗吧,下面將一些個人常用的代碼編輯器做一總結。題主已經說了編輯器,大家就不用在推薦VisualStudio、Eclipse之類的編譯器了。

1)VSCode

微軟出品,絕對的好用,跨平台支持,開源,易用性高。自己也是最近從之前常用的sublime切換到了VSCode,各方面還是很不錯的。很多習慣了的sublime快捷鍵可以很方便的無縫切換到沒稿VSCode。為什麼沒有推乎吵薦atom呢?理由很簡單,同作為使用JS開發的軟體,效率簡直甩atom幾條街。實在是無法忍受atom的卡頓。

2)Sublime

號稱最性感的編輯器,使用python開發,俗稱「SB」編輯器,不過其效率也是杠杠的,插件支持完善,對中文的支持可能需要我們單獨安裝插件ConvertToUTF8。雖然它是收費的,不過我們可以很方便的在網上找到注冊碼。和前文提到的VSCode相同,它也是一個跨平台的編輯器,同時支持Windows、Linux、MacOSX等操作系統。

3)Vim

號稱編輯器之神,搭配各種插件可以達到很好的效果。GVim是其在Windows平台下的特定版本,搭配YouCompleteMe插件來寫代碼,功能簡直爽的不要不要的。

4)Notepad++

Notepad++是免費軟體,可以免費使用,自帶中文,支持眾多計算機程序語言,支持語法高亮,也有語法折疊功能,比windows自帶的記事本功能不知道歲察侍強大了多少倍。

5)UltraEdit

據稱是黑客必備編輯器之一,可以編輯文本、十六進制、ASCII碼,該軟體又附有HTML標簽顏色顯示、搜尋替換及無限制還原功能,可修改EXE或DLL文件。下面是一張使用UltraEdit打開exe查看其內部數據的截圖。

最後,就不再推薦Emacs了,原因很簡單,這玩意上手太過陡峭,自己一直無緣使用,如果你有更好的編輯器推薦,歡迎討論。

熱點內容
短暫的訪問 發布:2024-09-23 01:29:08 瀏覽:827
安卓手機怎麼設置4位數 發布:2024-09-23 01:24:58 瀏覽:547
5g和演算法 發布:2024-09-23 01:20:40 瀏覽:506
pandas開發程序編譯 發布:2024-09-23 01:13:58 瀏覽:464
android應用被系統回收 發布:2024-09-23 01:10:36 瀏覽:446
c語言餐廳管理系統 發布:2024-09-23 01:01:35 瀏覽:93
linux系統新加網卡怎麼配置 發布:2024-09-23 01:01:34 瀏覽:893
java自我介紹一分鍾 發布:2024-09-23 00:51:34 瀏覽:672
告訴我解除密碼是多少 發布:2024-09-22 23:58:11 瀏覽:755
預演算法實施條例新 發布:2024-09-22 23:56:36 瀏覽:540