473,387 Members | 1,388 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,387 software developers and data experts.

Binary file output using python

Hi,
Is there a way in python to output binary files? I need to python to
write out a stream of 5 million floating point numbers, separated by
some separator, but it seems that all python supports natively is string
information output, which is extremely space inefficient.

I'd tried using the pickle module, but it crashed whenever I tried using
it due to the large amount of data involved.

Thanks for your help!
Apr 17 '07 #1
7 6660
On Apr 17, 12:41 pm, Chi Yin Cheung <c-che...@northwestern.eduwrote:
Hi,
Is there a way in python to output binary files? I need to python to
write out a stream of 5 million floating point numbers, separated by
some separator, but it seems that all python supports natively is string
information output, which is extremely space inefficient.

I'd tried using the pickle module, but it crashed whenever I tried using
it due to the large amount of data involved.

Thanks for your help!
You can create a binary file by doing something like this:

f = open(r'filename, 'b')
f.write('1,2,3,4,5,6')
f.close()

See also: http://www.devshed.com/c/a/Python/Fi...ent-in-Python/

Have fun!

Mike

Apr 17 '07 #2
Chi Yin Cheung wrote:
Hi,
Is there a way in python to output binary files? I need to python to
write out a stream of 5 million floating point numbers, separated by
some separator, but it seems that all python supports natively is string
information output, which is extremely space inefficient.
I recommend using PyTables for this sort of thing. It also allows you to
choose from several compression algorithms. I'm using it to store files
with 22000 x (2000, 12) datasets, or 528 million Float64s.
--
Michael Hoffman
Apr 17 '07 #3
Michael Hoffman wrote:
Chi Yin Cheung wrote:
>Hi,
Is there a way in python to output binary files? I need to python to
write out a stream of 5 million floating point numbers, separated by
some separator, but it seems that all python supports natively is
string information output, which is extremely space inefficient.

I recommend using PyTables for this sort of thing. It also allows you to
choose from several compression algorithms. I'm using it to store files
with 22000 x (2000, 12) datasets, or 528 million Float64s.
Addendum: it should also deal with endianness issues I wouldn't want to
handle myself, so your code will also be portable.
--
Michael Hoffman
Apr 17 '07 #4
Den Tue, 17 Apr 2007 11:07:38 -0700 skrev kyosohma:
On Apr 17, 12:41 pm, Chi Yin Cheung <c-che...@northwestern.eduwrote:
>Hi,
Is there a way in python to output binary files? I need to python to
write out a stream of 5 million floating point numbers, separated by
some separator, but it seems that all python supports natively is
string information output, which is extremely space inefficient.
I don't understand. To me it seams like there is no space difference:

[thomas@localhost ~]$ python
Python 2.4.4 (#1, Oct 23 2006, 13:58:00)
[GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>f = open("test2", "w")
f.write(str(range(10**7)))
f.close()
f = open("test", "wb")
f.write(str(range(10**7)))
f.close()
[thomas@localhost ~]$ ls -l test test2
-rw-rw-r-- 1 thomas thomas 88888890 17 apr 22:28 test
-rw-rw-r-- 1 thomas thomas 88888890 17 apr 22:27 test2
[thomas@localhost ~]$
Apr 17 '07 #5
On Apr 17, 10:30 pm, Thomas Dybdahl Ahle <lob...@gmail.comwrote:
Den Tue, 17 Apr 2007 11:07:38 -0700 skrev kyosohma:
On Apr 17, 12:41 pm, Chi Yin Cheung <c-che...@northwestern.eduwrote:
Hi,
Is there a way in python to output binary files? I need to python to
write out a stream of 5 million floating point numbers, separated by
some separator, but it seems that all python supports natively is
string information output, which is extremely space inefficient.

I don't understand. To me it seams like there is no space difference:

[thomas@localhost ~]$ python
Python 2.4.4 (#1, Oct 23 2006, 13:58:00)
[GCC 4.1.1 20061011 (Red Hat 4.1.1-30)] on linux2
Type "help", "copyright", "credits" or "license" for more information.>>f = open("test2", "w")
>f.write(str(range(10**7)))
f.close()
f = open("test", "wb")
f.write(str(range(10**7)))
f.close()

[thomas@localhost ~]$ ls -l test test2
-rw-rw-r-- 1 thomas thomas 88888890 17 apr 22:28 test
-rw-rw-r-- 1 thomas thomas 88888890 17 apr 22:27 test2
[thomas@localhost ~]$
That's OK, but he might also take a look at the 'struct' module which
can solve the "stream of 5 million floating point numbers, separated
by
some separator" part of the issue ( if binary format is needed ). From
the python docs...
>>from struct import *
pack('hhl', 1, 2, 3)
'\x00\x01\x00\x02\x00\x00\x00\x03'
>>unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
(1, 2, 3)
>>calcsize('hhl')
8
Apr 17 '07 #6
Chi Yin Cheung wrote:
Is there a way in python to output binary files? I need to python to
write out a stream of 5 million floating point numbers, separated by
some separator, but it seems that all python supports natively is string
information output, which is extremely space inefficient.

I'd tried using the pickle module, but it crashed whenever I tried using
it due to the large amount of data involved.
A minimalistic alternative is array.tofile()/fromfile(), but pickle should
handle a list, say, of 5 million floating point numbers just fine. What
exactly are you doing to provoke a crash, and what does it look like?
Please give minimal code and the traceback.

Peter
Apr 18 '07 #7
Peter Otten <__*******@web.dewrote:
Chi Yin Cheung wrote:
Is there a way in python to output binary files? I need to python to
write out a stream of 5 million floating point numbers, separated by
some separator, but it seems that all python supports natively is string
information output, which is extremely space inefficient.

I'd tried using the pickle module, but it crashed whenever I tried using
it due to the large amount of data involved.

A minimalistic alternative is array.tofile()/fromfile(), but pickle should
handle a list, say, of 5 million floating point numbers just fine. What
exactly are you doing to provoke a crash, and what does it look like?
Please give minimal code and the traceback.
cPickle worked fine when I tried it...
>>L=map(float, range(5000000))
import cPickle
out=file("z", "wb")
cPickle.dump(L, out, -1)
out.close()
inp=file("z", "rb")
K=cPickle.load(inp)
inp.close()
import os
os.system("ls -l z")
-rw-r--r-- 1 ncw ncw 45010006 Apr 19 18:43 z
0
>>>
Indicating each float took 9 bytes to store, which is 1 byte more than
a 64 bit float would normally take.

The pickle dump / load each took about 2 seconds.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Apr 19 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Danielsen Brian \(IFR Contractor External\) | last post by:
Does anyone know how to do file navigation between drives? I know how to navigate up and down within a drive (for example the C:\ drive), but I don't know how to change drives. I'm looking for...
8
by: mrstephengross | last post by:
I want to find a way to embed a tar file *in* my python script, and then use the tarfile module to extract it. That is, instead of distributing two files (extractor.py and archive.tar) I want to be...
29
by: 63q2o4i02 | last post by:
Hi, I'm interested in using python to start writing a CAD program for electrical design. I just got done reading Steven Rubin's book, I've used "real" EDA tools, and I have an MSEE, so I know what...
2
by: blanny | last post by:
Here is what I'm doing: file.write((char *) obj_ref, sizeof *obj_ref); The output looks like a text file with no formating. Aren't binary files supposed to contain binary numbers? lol. What...
8
by: sarma19 | last post by:
Hi Experts, I am interested in creating some Report files for that using below lines of perl code to display out put in colour to distinguish different types of Errors use Term::ANSIColor;...
2
by: email.ttindustries | last post by:
Hello, I am a C/C++ newbie and I have a simple question concerning fast data writing to binary files. Are there any other faster methods than the standard write() method to write to binary data...
2
by: skulka3 | last post by:
Hello, I want to implement file downloads inside an authenticated web page, such that when a user clicks a link, the server side python code connects to a ftp server, downloads a relevant file...
1
by: alivip | last post by:
How to brows and select a directory of file by using python TKinter like brows to find attachment in email but I can select not only file but whole directory any help
27
by: Jeff | last post by:
Im trying to figure out why I cant read back a binary file correctly. I have the following union: #define BITE_RECORD_LEN 12 typedef union { unsigned char byte; struct { unsigned char type;...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...

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.