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

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语法有很多特殊的地方,以后还要慢慢学习


相关文档:

Python入门的36个例子 之 29

源代码下载:下载地址在这里
# 033
class Person:
age = 22
def sayHello(self): # 方法与函数的区别在于需要一个self参数
print 'Hello!'
# end of def
# end of class # 这是我良好的编程风格
p = Person()
print p.age
p.sayHello()

output:
22
Hello! ......

Python入门的36个例子 之 32

源代码下载:下载地址在这里
A Byte Of Python
中关于继承有这样的文字:
Suppose you want to write a program which has to keep track of the
teachers and students in a college. They have some common
characteristics such as name, age and address. They also have specific
characteristics such as sala ......

Python入门的36个例子 之 34

源代码下载:下载地址在这里
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
# ......

在Windows上安装Python+MySQL 的常见问题及解决方法

验证是否已经安装了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模块学习

  copy模块用于对象的拷贝操作。该模块非常简单,只提供了两个主要的方法:
copy.copy

copy.deepcopy
,分别表示浅复制与深复制。什么是浅复制,什么是深复制,网上有一卡车一卡车的资料,这里不作详细介绍。复制操作只对复合对象有效。用简单的例子来分别介绍这两个方法。
浅复制只复制对象本身,没有复制该对象 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号