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
相关文档:
源代码下载:下载地址在这里
# 023
# Tuple(元素组)是不可变的列表
tuple1 = (1, 2, 3)
print tuple1
tuple2 = (tuple1, 4, 5, 6) # 一个元素组可以作为另外一个元素组的元素
print tuple2 # 并且能够在存储的时候保持原始的逻辑关系
for ele in tuple2:
print ele
print '\n'
for ele in tuple2[0]:
pr ......
源代码下载:下载地址在这里
# 035
class Person:
population = 0 #这个变量是属于整个类的
def __init__(self, name):
self.name = name
print '初始化 %s' % self.name
Person.population += 1
# end of def
def __del__(self):
print '%s says bye.' % self. ......
源代码下载:下载地址在这里
raise有两个参数,第一个是由我们自己定义的异常类型,第二个是关于此异常的少量说明信息。
# 038
def getAge():
age = input('Input your age:')
if (age < 0 or age > 160):
raise 'BadAgeError', 'It is impossible!!!!!'
# end of if
return age
# ......
# 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:
> ......
可以播放大部分的音视频.
demo download: http://www.sandy1219.com/python/media.rar
playMP3.py
# -*- coding: utf-8 -*-
import wx;
import wx.media;
import os;
import SPrint;
import mediaStateBar;
import mediaList;
import SaveLog;
import MediaItem;
woldcart = "media files|*.*|avi ......