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
相关文档:
#快速排序
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 ......
#使用类
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 ......
我的环境是:Linux version 2.4.21-4.EL
(bhcompile@daffy.perf.redhat.com) (gcc version 3.2.3 20030502 (Red Hat
Linux 3.2.3-20)) #1 Fri Oct 3 18:13:58 EDT 2003 + Python2.6.4
本文结合我安装时候的问题,总结而成
用户目录如/home/liuguanyu/ , 保证用户有root权限
1,看看有没有安装
&nbs ......
Python最大的特点就在于她的快速开发功能。作为一种胶水型语言,python几乎可以渗透在我们编程过程中的各个领域。这里我简单介绍一下用python进行gui开发的一些选择。
1.Tkinter
Tkinter似乎是与tcl语言同时发展起来的一种界面库。tkinter是python的配备的标准gui库,也是opensource的产物。Tkinter可用于win ......
时常见到一些好的Python模块,如果不随时记下,等用的时候又是一阵乱翻,好在Python引用一个模块极其方便,OK,废话少说:
1. decimal
标准模块,2.4引入,用于浮点数的精确表示:
>>> 0.1 + 0.1 + 0.1 - 0.3
5.5511151231257827e-17
>>> from decimal import Decimal
>>> def _(x): retu ......