- 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
- #该片段来自于http://www.codesnippet.cn/detail/140720149956.html
来源: http://www.codesnippet.cn/detail/140720149956.html