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

python读写二进制文件

初学python,现在要读一个二进制文件,查找doc只发现file提供了一个read和write函数,而且读写的都是字符串,如果只是读写char等一个字节的还行,要想读写如int,double等多字节数据就不方便了。在网上查到一篇贴子,使用struct模块里面的pack和unpack函数进行读写。下面就自己写代码验证一下。
>>> from struct import *
>>> file = open(r"c:\debug.txt", "wb")
>>> file.write(pack("idh", 12345, 67.89, 15))
>>> file.close() 
接着再将其读进来
>>> file = open(r"c:\debug.txt", "rb")
>>> (a,b,c) = unpack("idh",file.read(8+8+2))
>>> a,b,c
(12345, 67.890000000000001, 15)
>>> print a,b,c
12345 67.89 15
>>> file.close() 
在操作过程中需要注意数据的size


相关文档:

python算法实践2 shell排序

#shell排序
def ShellPass(mylist, d):
size = len(mylist)
i = d
while i < size:
if mylist[i] < mylist[i - d]:
tmp = mylist[i]
j = i - d
mylist[j + d] = mylist[j]
j = j - d
while j >= 0 and mylist[j] > ......

python算法实践4 快速排序

#快速排序
def Partition(mylist, low, high):
tmp = mylist[low]
while low < high:
while low < high and mylist[high] >= tmp:
high = high - 1
if low < high:
mylist[low] = mylist[high]
low = low + 1
while low < hi ......

python算法实践6 堆排序

#堆排序
def Heapify(mylist, start, end):
left = 0
right = 0
maxv = 0
left = start * 2
right = start * 2 + 1
while left <= end:
maxv = left
if right <= end:
if mylist[left] < mylist[right]:
maxv = right
......

python学习1 使用类

#使用类
class CPerson:
#类变量好比C++中的静态成员变量
population = 0

def SayHi(self):
print('Hello World')
def HowMany(self):
if CPerson.population == 1:
print('I am the only person here.')
else:
print(('We have %d perso ......

在Windows里搭建Python的GTK+环境搭建注意事项

在Windows里搭建Python的GTK+环境还是比较麻烦的有以下几个注意事项
1、PyGTK网站上下的包,可能没有Glade类(Lib\site-packages\gtk-2.0\gtk目录下没有glade.pyd),如果没有这个类你就无法在程序里导入Glade工具创建的xml,手写界面还是挺麻烦的。
2、GTK网站上的GTK包,没有包括Glade的DLL文件,还是无法读入Glade的xm ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号