[Tutor] simular function to pack (perl)
Michael P. Reilly
arcege@shore.net
Mon, 22 Jan 2001 12:11:58 -0500 (EST)
> Hello erverybody,
>
> are there simular function to pack and unpack (perl) in python.
>
> Thanks
>
> Wolfgang
In Python, it is the struct module.
import struct
datastruct = 'ff2xh' # the data structure format
s_sz = struct.calcsize(datastruct) # how many bytes in the structure
f = open('form.dat', 'rb')
graph_points = {}
block = f.read(s_sz) # read the first structure from the binary file
# into a character string
while block:
# decode the structure from the character string
(x, y, count) = struct.unpack(datastruct, block)
graph_points[x,y] = count
block = f.read(s_sz)
f.close()
# do something with graph_points
References:
Python Library Reference manual, section 4.3 - struct
<URL: http://www.python.org/doc/current/lib/module-struct.html>
-Arcege
--
------------------------------------------------------------------------
| Michael P. Reilly, Release Manager | Email: arcege@shore.net |
| Salem, Mass. USA 01970 | |
------------------------------------------------------------------------