http://www.oschina.net/code/snippet_1791760_37169 2014
本例实现直接中二进制文件读取原始数据并写入新的数组,且把字节转换成适当的类型。
1. [文件]
array.py ~ 486B 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import array
import binascii
import tempfile
a = array.array( 'i' , xrange ( 5 ))
print 'A1:' , a
#write the array of numbers to a temporary file
output = tempfile.NamedTemporaryFile()
a.tofile(output. file )
output.flush()
#read the raw data
with open (output.name, 'rb' ) as input :
raw_data = input .read()
print 'Raw Contents:' ,binascii.hexlify(raw_data)
#read the data into an array
input .seek( 0 )
a2 = array.array( 'i' )
a2.fromfile( input , len (a))
print 'A2:' , a2
|
|