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字典排序
1、
准备知识:
在python里,字典dictionary是内置的数据类型,是个无序的存储结构,每一元素是key-value对:
如:dict = {‘username’:‘password’,‘database’:‘master’},其中‘username’和& ......
我的环境是: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 ......
参考链接:http://www.woodpecker.org.cn/diveintopython/functional_programming/dynamic_import.html
一 动态导入模块
Python的import不能接受变量,所以应该用 __import__函数来动态导入。
如下的代码无法正常导入模块
modules = ['OpenSSL', 'Crypto', 'MySQLdb', 'sqlite3', 'zope.interface', 'pyasn1', 'twisted ......
1.列表的递归---用于输出列表字符串中的每个元素 >>> def printList(L):
#如果为空,则什么都不做
if not L:
return
#如果是链表,则对第一个元素调用printList函数
& ......
写了个图片蜘蛛人玩玩,抓了几个网页试试,感觉不不错。核心的代码可能20行也不到,简洁明了,嘻嘻。废话少说,翠花,上代码~~
#coding=utf-8
import os
import sys
import re
import urllib
URL_REG = re.compile(r'(http://[^/\\]+)', re.I)
IMG_REG = re.compile(r'<img[^>]*?src=([ ......