Python 二进制文件读取显示
filename=raw_input('enter file name:')
f=open(filename,'rb')
f.seek(0,0)
index=0
for i in range(0,16):
print "%3s" % hex(i) ,
print
for i in range(0,16):
print "%-3s" % "#" ,
print
while True:
temp=f.read(1)
if len(temp) == 0:
break
else:
print "%3s" % temp.encode('hex'),
index=index+1
if index == 16:
index=0
print
f.close()
这里显示的是,读取一个BMP图像后的效果
从这里,可以看出,print语句和C的printf对格式要求是一致的,或者说,Python采用了C的格式规范。
print "%-3s" % "#" ,
逗号防止自动生成换行符,-3表示显示占3个字符并且从左显示(默认从右)。
f.read(1)
每次读一个字节。如果读出来的长度为0,则到了文件末尾。
Python语法有很多特殊的地方,以后还要慢慢学习
相关文档:
源代码下载:下载地址在这里
# 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. ......
验证是否已经安装了MySQLdb:
==========================================================
d:\usr\local\Python25>python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] onwin32
Type "help", "copyright", "credits" or "license" for ......
Python Firewall Win32 (pyfw-win32)
pyfw-win32是一个可用Python脚本开发数据包过滤(防火墙)的模块。底层使用C语言编写的NDIS中间层驱动(NDIS IMD)提供支持,上层提供Python开发接口。可用Python脚本处理所有逻辑问题,而不必关心底层实现,达到快速、灵活开发的目的。
Google 项目托管:
http://code.google.com/p/py ......
——由于最近在做有关网页搜索的项目,涉及到一些编码方面的知识,小弟在网上偶然地发现了这么一篇文章,很易懂,不晦涩,为了方便自己也同时能方便大家,就转了过来,以作参考……
文章出处:http://blog.csdn.net/tingsking18/arc ......