473,605 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

puting floats into a Binary file

3 New Member
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 9561
bartonc
6,596 Recognized Expert Expert
You may find a suitable solution here.
Aug 23 '07 #2
swordman
3 New Member
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 Recognized Expert New Member
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
swordman
3 New Member
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
1408
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 floats).
2
1332
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 (except possibly for performance)? Thanks, Kartik.
2
3625
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; http://groups.google.com/group/comp.lang.python/browse_thread/thread/42150ccc20a1d8d5/4aadc71be8aeddbe#4aadc71be8aeddbe I copied this code and modified it slightly, however, you will notice that for one of the examples, the conversion isn't...
4
1803
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 float. For example: > (coerce 2/3 'single-float) 0.6666667 > (coerce 2/300 'single-float) 0.006666667
6
5458
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 differently than Java does. I suspected an endianess problem and tried out ntohl/htonl but it doesn't help. Any clues?
2
3168
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 a normal text file. I need to read in a float from the binary file, but it sets my input stream to failbit. Is there a special way I can read in floats? All I am doing is
23
2233
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 moment I use this terrible substitute: FLOAT_PREC = repr(1.0/3).count('3')
16
4124
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 for float()" " here's the code ************
3
2333
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
7934
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8424
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8069
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8286
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6742
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5445
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3912
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2438
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1537
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.