By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,761 Members | 1,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,761 IT Pros & Developers. It's quick & easy.

puting floats into a Binary file

P: 3
I'me new here, so I wish to say HELLO to all members.
I'm quite new to python, but I manage to do something using python in blender.
Now, I need to save in a binary file raw data (specificaly blocks of 3 float), for a later use. How can I manage this? i tried the write() function, but it need the conversion to string, I need the value! Thanks all!
Aug 23 '07 #1
Share this Question
Share on Google+
4 Replies

bartonc
Expert 5K+
P: 6,596
You may find a suitable solution here.
Aug 23 '07 #2

P: 3
Thanks for the reply.
I've already take alook at that thread, but, as far I can understand it, they are reading from a file. I need to write the data in it.
I suppose thet I've to use the struct.pack in some way, but I cant figure out how
Aug 23 '07 #3

dshimer
Expert 100+
P: 136
I use the struct functions all the time for this very thing, though rarely see them talked about here. Probably because I'm missing out on an easy way to do things. I did though write a script as an example for my blog. It may or may not be useful but, goes like this.

Expand|Select|Wrap|Line Numbers
  1. print 'testbinary.py modified 11:00 PM 7/10/2007'
  2. '''
  3. Copyright 2007 Dennis Shimer
  4. Vr Mapping copyright Cardinal Systems, LLC
  5. No warranties as to safety or usability expressed or implied.
  6. Free to use, copy, modify, distribute with author credit.
  7.  
  8. just a lesson in binary data. Either run from a command line 3 times like
  9. python testbinary.py
  10. or open or paste into pyedi and execute 3 times.
  11. '''
  12.  
  13. import os,struct
  14. # We'll use os to test for the existence of the file, an struct to 
  15. # interact with the binary data.
  16.  
  17. FormatString='10sddi'
  18. '''
  19. The format string tells several parts of the program what we are working
  20. with in regards to binary data. The format string listed above represents
  21. a 10 character string, 2 double precision real numbers, and an integer.
  22. Same as before but I added the int to show by example. Note that it is
  23. just a string with letters representing different kinds of data.
  24. '''
  25.  
  26. ParFileName='testbinary.par'
  27. # The file by this name will show up in the directory the script is run in.
  28.  
  29. print 'Bytes occupied by binary data',struct.calcsize(FormatString),'\n\n'
  30. '''
  31. Sometimes you need to know how many bytes to read from a file in order
  32. to get one set of data when multiple sets exist, a good example goes
  33. back to the coordinate file mentioned earlier. In order to read and process
  34. one point at a time you can only read the necessary bytes. the calcsize
  35. method in struct will read a format string and tell how many bytes are
  36. represented. You can go into a python interpreter, import struct then
  37. evaluate some single item format strings like the following for a
  38. float, a double, an int, and a 1 character string.
  39. >>> struct.calcsize('f')
  40. 4
  41. >>> struct.calcsize('d')
  42. 8
  43. >>> struct.calcsize('i')
  44. 4
  45. >>> struct.calcsize('s')
  46. 1
  47. '''
  48.  
  49. if not os.path.isfile(ParFileName): # If the file doesn't exist.
  50.     ParFile=open(ParFileName,'wb')
  51.     # Then open it in write (create or erase) mode with a binary modifier
  52.  
  53.     BinaryData=struct.pack(FormatString,'Word',.2,.0,0)
  54.     '''
  55.     The pack method in the struct module uses the format string to process
  56.     the data that comes next into the bytes which represent them in binary
  57.     form. It is important that the amount and type of data following the
  58.     format string match, notice I am passing in a string (less than 10
  59.     characters allowed, 2 real numbers and an integer.
  60.     '''
  61.  
  62.     ParFile.write(BinaryData)
  63.     ParFile.close()
  64.     # We have a binary file and binary data so just and write and close it.
  65.  
  66.     print 'Program run with no existing file'
  67.     print 'Data set to defaults.'
  68.     print 'testbinary.par should now exist.'
  69. else:
  70.     ParFile=open(ParFileName,'r+b')
  71.     # If it does exist open for reading + writing with binary modifier.
  72.  
  73.     BinaryData=ParFile.read()
  74.     '''
  75.     This time since the file exists we are going to get the binary data
  76.     right out of the file with a read. Since we want the whole file there 
  77.     is no reason to use calcsize to tell it how many bytes to read.
  78.     '''
  79.  
  80.     DataList=struct.unpack(FormatString,BinaryData)
  81.     # Going the other direction, lets use unpack to use the format string
  82.     # to convert the binary data into a list of values.
  83.  
  84.     print 'Data from file, note it is a list\n   ',DataList
  85.     # and print the list.
  86.  
  87.     ParFile.seek(0,0)
  88.     '''
  89.     Now assuming that there are new values that need to be used to update
  90.     the parameter file. The first thing we need to do is go back to the
  91.     beginning of the file, read up on seek, but this basically moves you
  92.     0 bytes from the beginning. If in the coordinate file example I had a
  93.     data structure that require 24 bytes and wanted to get the 3rd value 
  94.     from the file I could to the 48th byte and read 24. This way you can
  95.     read, write, and overwrite data in place within a file.
  96.     '''
  97.  
  98.     print 'File position after seek ',ParFile.tell()
  99.     # Every open file has a tell method which gives the current position.
  100.  
  101.     BinaryData=struct.pack(FormatString,'Updated',1,2,3)
  102.     ParFile.write(BinaryData)
  103.     # Same idea as above, just using the changed data (wherever it came from)
  104.  
  105.     if DataList[0].split('\x00')[0]=='Updated':
  106.         print 'You have seen it all.'
  107.         print 'Remove testbinary.par to start over'
  108.         '''
  109.         There may be a better way to convert this 10 character string to
  110.         what you normally think of as a string, but it comes back padded
  111.         with the 0 bytes and this works simply enough. Think of it this
  112.         way 'Updated\x00\x00\x00' has 3 extra bytes, if you split on that
  113.         character you get back ['Updated', '', '', ''] of which 'Updated'
  114.         is the first or [0] element. Ugly but I've never really look into
  115.         a better way.
  116.         '''
  117.  
  118.     else:
  119.         print 'Program run with an existing file'
  120.         print 'Data was updated run again to see change'
  121.         # 2nd run string doesn't equal 'Updated' so print appropriate message.
  122.  
  123.     ParFile.close()
  124.     # Good form to explicitely close files.
  125.  
  126.  
Thanks for the reply.
I've already take alook at that thread, but, as far I can understand it, they are reading from a file. I need to write the data in it.
I suppose thet I've to use the struct.pack in some way, but I cant figure out how
Aug 23 '07 #4

P: 3
Thanks a lot! It is exactly what I'm looking for! And a very good explanation too.
Thanks again.
Aug 24 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.