473,406 Members | 2,343 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,406 software developers and data experts.

Write 10k integers to a file using struct

What's the quickest way to write and read 10.000 integer values ( or
more ) to and from a file? Using struct somehow? The example in the
docs shows how to handle to or three arguments, but is the fastest way
to write and read just a simple :

while data:
f.write(struct.pack(format, integer-value))

or is there another, quicker and nicer way to do it????

Thomas
Jul 18 '05 #1
2 3065
>>>>> "Thomas" == Thomas <20**@weholt.org> writes:

Thomas> What's the quickest way to write and read 10.000 integer
Thomas> values ( or more ) to and from a file? Using struct
Thomas> somehow? The example in the docs shows how to handle to or
Thomas> three arguments, but is the fastest way to write and read
Thomas> just a simple :

Thomas> while data: f.write(struct.pack(format, integer-value))

Thomas> or is there another, quicker and nicer way to do it????

When handling arrays of numbers of that size, you may want to think
about using numeric/numarray. In the example below, I use randint to
simply to create a list of random integers for demo purposes.

1 >>> import random

2 >>> nums = [random.randint(0,100000) for i in range(10000)]

3 >>> from numarray import array, fromfile

4 >>> a = array(nums)

5 >>> a.typecode
----> a.typecode()
Out[5]: 'l'

6 >>> a.tofile?
Type: instancemethod
Base Class: <type 'instancemethod'>
String Form: <bound method NumArray.tofile of array([ 5193, 43059, 91249, ..., 13735, 38070, 75129])>
Namespace: Interactive
File: /usr/local/lib/python2.3/site-packages/numarray/generic.py
Definition: a.tofile(self, file)
Docstring:
Write the array as a binary image to a file.

If file is a string, it attempts to open a file with that name,
otherwise it assumes file is a file object. At the moment if
special positioning is needed in the file one must do that with
the file object beforehand. More options may be added to this
method to allow positioning or appends.

Note that for numerical data, the system byte order in which
the data is represented is *not* recorded in the file. This
renders the file non-portable because extra information is
required to interpret it on different machines than the one it
was created on.
7 >>> a.tofile('mynums.dat')

8 >>> fromfile?
Type: function
Base Class: <type 'function'>
String Form: <function fromfile at 0xb4b2c614>
Namespace: Interactive
File: /usr/local/lib/python2.3/site-packages/numarray/numarraycore.py
Definition: fromfile(file, type, shape=None)
Docstring:
Create an array from binary file data

If file is a string then that file is opened, else it is assumed
to be a file object. No options at the moment, all file positioning
must be done prior to this function call with a file object
9 >>> b = fromfile('mynums.dat', 'l')

10 >>> b.shape
Out[10]: (10000,)

11 >>> a is b
Out[11]: False

12 >>> a==b
Out[12]: array([1, 1, 1, ..., 1, 1, 1], type=Bool)

Jul 18 '05 #2

Thomas <20**@weholt.org> wrote:
What's the quickest way to write and read 10.000 integer values ( or
more ) to and from a file? Using struct somehow? The example in the
docs shows how to handle to or three arguments, but is the fastest way
to write and read just a simple :

while data:
f.write(struct.pack(format, integer-value))

or is there another, quicker and nicer way to do it????


Look at the array module.

- Josiah

Jul 18 '05 #3

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

Similar topics

6
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
27
by: Sune | last post by:
Hi! Pre-requisites: ------------------- 1) Consider I'm about to write a quite large program. Say 500 K lines. 2) Part of this code will consist of 50 structs with, say, no more than at most...
3
by: Dae-Suk Chung | last post by:
Hi, What is the easiest way to write a struct(with biary data) to a file ? do I need to covert it through e.g. Marshal class ? Thanks for any tips! DaeSuk.
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
7
by: nass | last post by:
hi all, i am running slackware linux and need to use some function that will will enable me to write and read from a shared mem segment.. i am using open() , to open a file, and then use mmap to...
4
by: nickcleary1 | last post by:
I need to pass 4 integers between two forms but am having trouble passing more than one.I am hoping to use the contructor approach as i need the variables straight away Any help would be great ...
24
by: Bill | last post by:
Hello, I'm trying to output buffer content to a file. I either get an access violation error, or crazy looking output in the file depending on which method I use to write the file. Can anyone...
12
by: Monica Leko | last post by:
Hi I have a specific format and I need binary representation. Does Python have some built-in function which will, for instance, represent number 15 in exactly 10 bits?
13
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.