473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

puting floats into a Binary file

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
4 9537
bartonc
6,596 Expert 4TB
You may find a suitable solution here.
Aug 23 '07 #2
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
136 Expert 100+
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
Thanks a lot! It is exactly what I'm looking for! And a very good explanation too.
Thanks again.
Aug 24 '07 #5

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

Similar topics

0
by: Thinkit | last post by:
Are there any good libraries of arbitrary precision binary floats? I'd like to specify the mantissa length and exponent range. Hexadecimal output would be best (decimal is disgusting with binary...
2
by: kartik | last post by:
Since the Decimal type can represent any given fractional number exactly, including all IEEE binary floating-point numbers, wouldn't it be advisable to use Decimals for all floating point numbers...
2
by: geskerrett | last post by:
In the '80's, Microsoft had a proprietary binary structure to handle floating point numbers, In a previous thread, Bengt Richter posted some example code in how to convert these to python floats;...
4
by: Adam Warner | last post by:
Hi all, I'm used to ANSI Common Lisp implementations sanely printing single and double floats. By sane I mean the printed decimal representation mimics the underlying binary precision of the...
6
by: sbalko | last post by:
Hi, I am trying to read Java-floats (IEEE 754 encoding) stored in a binary file from C (gcc on linux/i386, more specifically). Unfortunately, C seems to expect floats to be stored somewhat...
2
by: Matt McGonigle | last post by:
Hi all, Please help me out with this. Perhaps it is a dumb question, but I can't seem to make it work. I am doing a file conversion using an unformatted binary file for input and outputting to...
23
by: Arnaud Delobelle | last post by:
Hi all, I want to know the precision (number of significant digits) of a float in a platform-independent manner. I have scoured through the docs but I can't find anything about it! At the...
16
by: luca bertini | last post by:
Hi, i have strings which look like money values (ie 34.45) is there a way to convert them into float variables? everytime i try I get this error: "numb = float(my_line) ValueError: empty string...
3
by: M.-A. Lemburg | last post by:
On 2008-08-07 20:41, Laszlo Nagy wrote: 1 It also very fast at dumping/loading lists, tuples, dictionaries, floats, etc. -- Marc-Andre Lemburg eGenix.com
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.