file = open("blah","r")
data = file.read(4096)
File Buffer to Struct
Hello,
I'm trying to convert the C/C++ code found here: http://johanneskopf.de/publications/solid/textures/file_format.txt into Python.
It looks like the file is loaded, and the header file is read off into a C struct. The contens of the file are then read. I have no idea how to approach this in Python, apart from simply opening the file and reading the first 4096 bytes:
Here's a test file that can be used to test the loader: http://johanneskopf.de/publications/solid/textures/woodwall.vol.
Help, anyone?
Thanks,
Geometrian
You can use the struct module to read binary data into Python objects. For example:
import structfile = open("woodwall.vol", "rb")data = file.read(4096)FORMAT_STRING = "4si256s?3i"fields = struct.unpack(FORMAT_STRING, data[:struct.calcsize(FORMAT_STRING)])class VolumeHeader: passheader = VolumeHeader()header.magic, header.version, header.texName, header.wrap, header.volSize, header.numChannels, header.bytesPerChannel = fields# Convert 'bytes' objects into stringsheader.magic = header.magic.decode("ascii")header.texName = header.texName[:header.texName.find(b"\0")].decode("ascii")
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement