以下在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 "", line 1,

in

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 "", line 1, in

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 "", line 1, in

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 "", line 1, in

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 "", line 1, in

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

python3怎么使用qstring_请问PyQt的QString和python的string的区别?相关推荐

  1. python3怎么使用qstring_PyQt的QString和python的string的区别

    python string和PyQt的QString的区别 以下在Python2.6和PyQt4.4.4 for Python2,6环境下讨论: Python中有两种有关字符的类型:Python st ...

  2. python3 上传文件到目标机器_再见Python 2.7,你好Python 3.7

    安装python3 直接下载安装 1.下载地址如下:python 3.7 2.点击下载好的pkg文件进行安装 3.安装完成之后,python 3.70的默认安装路径如下: /Library/Frame ...

  3. python死机_请问下为什么我用PYTHON写编译器一旦用READ就死机

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 请问下为什么我用PYTHON写编译器一旦用READ就死机 read代码如下: def praseRead(self, fsys, lev): self.n ...

  4. 使用PyQt开发图形界面Python应用专栏目录

    ☞ ░ 前往老猿Python博文目录 ░ 本专栏为收费专栏的文章目录,对应的免费专栏为<PyQt入门知识目录>,两个专栏都为基于PyQt的Python图形界面开发基础教程,只是收费专栏中的 ...

  5. PyQt PySide版本与Python版本选择

    (经过验证如果要用matplotlib则不建议安装pyside6.4版本,6.3.2版本测试嵌入matplotlib成功 python版本3.8 3.9测试ok,3.10未测试) PyQt最新版本信息 ...

  6. python3.8安装requests库_再见requests!Python下一代 http客户端出炉啦!

    小编玩Python大概是3-4年前,那个时间爬虫非常流行,到处都是爬虫的文章和视频,很多人入门Python都是从爬虫开始.几乎所有学Python都知道requests这个库,它大名鼎鼎K神的得意之作, ...

  7. python3中字符串编码常见种类_Python基础篇—标准数据类型—String字符串编码问题...

    我要开始写String编码问题了...脑壳疼.. 在String字符串的第一篇末尾有留一个坑,就是关于中文字符串编码.整个编码的故事说起来都是很费劲的,我也只能把我所知道的梳理整理一下,在日常敲码过程 ...

  8. ubuntu安装python3.8_将 Ubuntu 16 和 18 上的 python 升级到最新 python3.8 的方法教程

    1. 概述 本文记录在 Ubuntu 16.04 上将 python 升级为 3.8 版本,并配置为系统默认 python3 的过程. 在 Ubuntu 16.04 中,python3 的默认版本为 ...

  9. pythonmessage用法_请问Mac下如何用python读取iMessage信息?

    很早之前,学习Python web编程的时候,就涉及一个Python的urllib.可以用urllib.urlopen("url").read()可以轻松读取页面上面的静态信息.但 ...

最新文章

  1. 组原,汇编语言关于代码段的定义
  2. QTP中对数据库的操作(查询,更新和删除等)
  3. CVPR 2022 | 室外多模态3D目标检测(DeepFusion)
  4. layui table is not a valid module
  5. http://www.openwebx.org/docs/turbine.html
  6. 【推荐】查找一代用户出口Userexit
  7. wireshark如何对指定ip进行嗅探
  8. Serverless 时代前端避坑指南
  9. 阿里再开源!基于JAVA的模块化开发框架JarsLink
  10. php mysql 实现原理_php+mysql分页原理实现
  11. 在计算机组成原理中x,计算机组成原理xu2.ppt
  12. 使用kafka解决zookeeper is not a recognized option when executing kafka-console-consumer.sh报错
  13. java基础学习-(1)面向对象
  14. 找出js里面改变cookies的代码
  15. oracle 对比 clob,解决比较Oracle中CLOB字段问题
  16. 考研数据库系统概论题目整理
  17. Java异或性能测试速度为2.5GiB/s
  18. 使用netron实现对onnx模型结构可视化
  19. OpenCV3.1.0安装在ubuntu16.40(Linux)步骤(电赛AM5708板子可用)
  20. 人性:《少年pi的奇幻漂流》和《一九四二》连看小感

热门文章

  1. jpa之PagingAndSortingRepository带分页查询
  2. 【JFreeChart】JFreeChart—输出组合图表
  3. IT兄弟连 Java语法教程 Java的发展历程
  4. Nest Secure智能保全系统内建麦克风 引发用户反弹
  5. P2253 好一个一中腰鼓!
  6. 94 Binary Tree Inorder Traversal
  7. 用中断例程完成loop指令的功能【安装中断例程,设置中断向量表】
  8. 活动目录网域中禁用移动存储(U盘)
  9. 日志管理:(三)配置_JCL+Log4J中遇到的问题
  10. 一份数据分析学习清单.xls