`
metallica_1860
  • 浏览: 33193 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

python是个什么东西---python---python string和PyQt的QString的区别

阅读更多

 

python string和PyQt的QString的区别 以下在Python2.6和PyQt4.4.4 for Python2,6环境下讨论: Python中有两种有关字符的类型:Python string object和Python Unicode object。主要使用Python string object进行数据输入输出。 PyQt中与之相对应的字符有关类

 

 

python string和PyQt的QString的区别

 

以下在Python2.6和PyQt4.4.4 for Python2,6环境下讨论:

Python中有两种有关字符的类型:Python string object和Python Unicode object。主要使用Python string object进行数据输入输出。

PyQt中与之相对应的字符有关类型是:QByteArray和QString。主要使用QString操作数据。

 

 

1. Python和PyQt中的类型对应

 

 

注意是类型相似,不是相等。

需要先了解编码:ascii、gb2312、big5,这些是各国自己文字不同的编码;unicode,国际通用编码,就是穷尽这个世界上所有的文字,给每个文字编一个,又分utf-8方案--最常使用的128个英文字母用一个字节来表示,而中文使用三个字节来表示,utf-16方案--其中英文和中文都使用两个字节来表示,而其它字符采用四个字节,utf-32方案--所有的文字都用四个字节来表示。

unicode就可用来作为各种独立编码如ascii、gb2312、big5的转换中介。

Python中gkb == gb2312。

1)Python string object可以理解为一个接一个字节(byte,8位)的字节组,至于表示什么编码,与表示文字有关,如:"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 <module>

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 <module>

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 <module>

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 <module>

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 <module>

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

 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/angelipin/archive/2009/09/10/4537335.aspx

分享到:
评论

相关推荐

    python是个什么东西---python---pyqt做的一个浏览器

    "Python是个什么东西"这个问题,其实是在询问Python的基本概念以及其在不同领域的应用。 Python支持多种编程范式,包括面向对象、命令式、函数式和过程式编程。它内置了丰富的标准库,涵盖了网络、操作系统接口、...

    python-vlc-for-Pyqt5-master_ip摄像头_pyhton;vlc_pyqt5_pyqtvlc_vlc_源

    标题中的"python-vlc-for-Pyqt5...总的来说,这个项目展示了如何结合Python、VLC和PyQt5来实现一个实时IP摄像头监控应用,对于想要学习多媒体处理、GUI编程以及网络视频流的开发者来说,这是一个非常有价值的实践案例。

    Python库 | QT-PyQt-PySide-Custom-Widgets-0.1.8.tar.gz

    资源分类:Python库 所属语言:Python 资源全名:QT-PyQt-PySide-Custom-Widgets-0.1.8.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    python聊天室---pyqt5+socket+Thread聊天室

    综上所述,"python聊天室---pyqt5+socket+Thread聊天室"项目涵盖了Python GUI编程、网络通信和并发处理等多个核心概念。通过学习和实践该项目,开发者不仅能掌握PyQt5的使用,还能深入了解Socket编程和多线程技术,...

    基于Python-PyQt5饭卡管理系统.zip

    基于Python-PyQt5饭卡管理系统.zip基于Python-PyQt5饭卡管理系统.zip 基于Python-PyQt5饭卡管理系统.zip基于Python-PyQt5饭卡管理系统.zip 基于Python-PyQt5饭卡管理系统.zip基于Python-PyQt5饭卡管理系统.zip 基于...

    python3-pyqt5-sip-4.19.19-2.el8.aarch64.rpm

    官方离线安装包,亲测可用

    Python库 | PyQt5_sip-12.8.1-cp35-cp35m-win_amd64.whl

    PyQt5是Qt库的Python版本,Qt是一个跨平台的C++库,用于开发桌面、移动和Web应用。通过PyQt5,Python开发者可以利用Qt的强大功能,包括丰富的GUI元素、网络编程、多媒体处理、数据库支持等。SIP作为PyQt5的一部分,...

    《PYTHON QT GUI快速编程---PYQT编程指南》源码

    Python PyQt 是一个强大的GUI编程框架,它允许开发者使用Python语言来构建美观且功能丰富的图形用户界面。这本书《PYTHON QT GUI快速编程---PYQT编程指南》是基于英文原版《Rapid GUI Programming with Python and ...

    pyqt5和pyqt5-sip

    PyQt5是一个强大的Python绑定库,它使得Python程序员可以利用Qt框架进行图形用户界面(GUI)应用程序的开发。Qt框架是C++编写的一个跨平台的应用程序开发框架,广泛应用于Windows、Linux、macOS等操作系统。PyQt5是...

    python-pyqt5-demo

    这个"python-pyqt5-demo"项目很可能是包含了多个PyQt5应用示例,帮助开发者理解和学习如何使用PyQt5进行GUI开发。 PyQt5是Python与Qt库之间的桥梁,Qt库是一个跨平台的C++框架,广泛用于开发桌面、移动和嵌入式应用...

    PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7-x32

    PyQt4是一个强大的工具包,它允许Python程序员利用Qt库创建功能丰富的图形用户界面(GUI)应用程序。这个压缩包“PyQt4-4.11.4-gpl-Py2.7-Qt4.8.7-x32”是专门为在32位系统上运行Python 2.7设计的PyQt4版本,版本号...

    python QT GUI快速编程--pyqt编程指南

    Python QT GUI快速编程——PyQt编程指南是一本专注于教授如何使用Python和PyQt库进行图形用户界面(GUI)开发的教程。PyQt是Python与Qt库的接口,它允许开发者利用Python的简洁语法和Qt的强大功能来创建丰富的、交互...

    python3-pyqt5-sip-4.19.24-1.el8.x86_64.rpm

    官方离线安装包,测试可用。请使用rpm -ivh [rpm完整包名] 进行安装

    基于Python-PyQt5的饭卡管理系统源码+详细注释+数据库sql.zip

    基于Python-PyQt5的饭卡管理系统源码+详细注释+数据库.zip 基于Python-PyQt5的饭卡管理系统源码+详细注释+数据库.zip 基于Python-PyQt5的饭卡管理系统源码+详细注释+数据库.zip 基于Python-PyQt5的饭卡管理系统源码+...

    pyqt5-python-Gui

    pyqt5新手入门教程,以章节的形式加以讲述,每一章实现了一个功能

    Python3.4 PyQt5 32位安装版(PyQt5-5.5.1-gpl-Py3.4-Qt5.5.1-x32.exe)

    总的来说,"Python3.4 PyQt5 32位安装版"是一个强大的工具,它结合了Python的易用性和Qt的跨平台能力,为开发者提供了构建桌面应用程序的强大平台。无论是在个人项目还是企业级应用中,都能看到其广泛的应用。

    基于Python-PyQt5的饭卡管理系统源码+详细注释+数据库.zip

    【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为...基于Python-PyQt5的饭卡管理系统源码+详细注释+数据库.zip

    PYTHON QT GUI快速编程-PYQT编程指南

    PYTHON QT GUI快速编程-PYQT编程指南 基于PyQt4的技术文档

    python大作业新项目-基于PyQt5的音乐播放器python实现源码.zip

    python大作业新项目-基于PyQt5的音乐播放器python实现源码.zip 【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上传。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目...

    Qt-Python-Binding-Examples-master.zip_pyqt_python qt_python_qt_b

    标题中的“Qt-Python-Binding-Examples-master.zip”表明这是一个包含PyQT示例代码的压缩包,旨在帮助开发者理解和学习如何使用PyQT。 **PyQT概述** PyQT是Python的一个模块集合,它提供了对Qt库的完全访问,包括...

Global site tag (gtag.js) - Google Analytics