分享

读取二进制文件写入array

 心不留意外尘 2016-08-02
 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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约