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语法有很多特殊的地方,以后还要慢慢学习
相关文档:
# 027
toolName = 'Google'
if toolName.startswith('Go'):
print 'The tool\'s name starts with \"Go\".'
if 'oo' in toolName:
print 'The tool\'s name contains the stirng "oo".'
print toolName.find('gl') # 返回首次出现“gl”字符串的索引
if toolName.find('Baidu ......
源代码下载:下载地址在这里
# 032
# 其实cPickle这个模块起到的作用可以用“完美地协调了文件中的内容(对象)和代码中的引用”来形容
import cPickle as p # 这条语句给cPickle起了个小名p
objectFileName = r'C:\Data.txt'
aList = [1, 2, 3]
f = file(objectFileName, 'w')
p.dump(aList, f)
f.close ......
# 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 ......
——由于最近在做有关网页搜索的项目,涉及到一些编码方面的知识,小弟在网上偶然地发现了这么一篇文章,很易懂,不晦涩,为了方便自己也同时能方便大家,就转了过来,以作参考……
文章出处:http://blog.csdn.net/tingsking18/arc ......