易截截图软件、单文件、免安装、纯绿色、仅160KB

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
NutSh


相关文档:

Python入门的36个例子 之 18

例1:
# _018
# This is a module (if write Chinese in a module, there will be a error)
def func1():
print 'This is function 1.'
def func2():
print 'This is function 2.'
def func3():
print 'This is function 3.'
# 019
# 使用“import”语句调用模块:
import _018_Module
_ ......

Python入门的36个例子 之 19

源代码下载:下载地址在这里
# 022
listNum1 = [1, 3]
listNum2 = [2, 4]
listStr1 = ['a', 'c']
listStr2 = ['b', 'd']
# 列表的合并
list1 = listNum1 + listStr1
for ele in list1:
print ele
print '\n'
# 判断列表中是否包含某元素
print 'b' in list1
print 1 in list1
# 删除某个元素
for ele in ......

Python入门的36个例子 之 23

源代码下载:下载地址在这里
# 026
aList = ['1','2','3','4']
aListCopy = aList # 其实,这里仅仅复制了一个引用
del aList[0]
print aList
print aListCopy # 两个引用指向了了同一个对象,所以打印结果一样
aListCopy = aList[:] # 这是复制整个对象的有效方法
del aList[0]
print aList
print aListCopy
......

Python入门的36个例子 之 26

源代码下载:下载地址在这里
# 029
aFile = file(r'C:\in.txt', 'r')
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
print line,
# end of while
aFile.close()

output:
>>>
This is the first line.
This is the second line.
This is ......

Python入门的36个例子 之 36

# 040
import time
try:
f = file('040_Finally.py')
while True:
line = f.readline()
if len(line) == 0:
break
time.sleep(0.33)
print line,
# end of while
finally:
f.close()
print 'Closed the file.'
# end of try
output:
> ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号