[Tutor] reading binary files
etrade.griffiths at dsl.pipex.com
etrade.griffiths at dsl.pipex.com
Tue Feb 3 10:20:36 CET 2009
Sorry, still having problems ....
> > I am trying to read data from a file that has format
> > item_name num_items item_type items ....
> >
> > eg
> >
> > TIME 1 0.0
> > DISTANCE 10 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
>
> Where is the item_type?
Ooops, the data format should look like this:
TIME 1 F 0.0
DISTANCE 10 F 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
F=float, D=double, L=logical, S=string etc
>
> > I can read this if the data are in ASCII format using
> >
> > in_file = open("my_file.dat","r")
> > data1 = in_file.read()
> > tokens = data1.split()
>
> It might be easier to process line by line using readline
> or readlines rather than read but otherwise, ok so far...
>
> > and then stepping through the resulting list but the data
> > also appear in the same format in a binary file.
>
> When you say a binary file do you mean an ASCII file
> encoded into binary using some standard algorithm?
> Or do you mean the data is binary so that, for example,
> the number 1 would appear as 4 bytes? If so do you
> know how strings (the name) are delimited? Also
> how many could be present - is length a single or
> multiple bytes? and are the reors fixed length or
> variable? If variable what is the field/record separator?
Sorry, no idea what the difference is. All I know is that the data
were written by a FORTRAN program using the UNFORMATTED argument
in the WRITE statement and that if they had been written FORMATTED
then we would get afile that looks something like the example above
>
> You may need to load the file into a hex editor of debugger
> to determine the answers...
>
> Having done that the struct module will allow you to read
> the data.
>
> You can see a basic example of using struct in my
> tutorial topic about handling files.
The first part of the file should contain a string (eg "TIME"),
an integer (1) and another string (eg "F") so I tried using
import struct
in_file = open(file_name+".dat","rb")
data = in_file.read()
items = struct.unpack('sds', data)
Now I get the error
error: unpack requires a string argument of length 17
which has left me completely baffled!
>
>
> ------------------------------
>
> Message: 4
> Date: Mon, 02 Feb 2009 14:53:59 -0700
> From: Bernd Prager <bernd at prager.ws>
> Subject: [Tutor] question about mpmath product expression
> To: tutor at python.org
> Message-ID: <ac7e7f56dc4bc0903dc7df8861f9ba5c at prager.ws>
> Content-Type: text/plain; charset="UTF-8"
>
> Does anybody know if there is a precision difference when I use mpmath and
> take an expression:
>
> from mpmath import *
> mp.dps = 100
> mu0 = [mpf('4') * pi * power(10, -7)
>
> rather then:
>
> mu0 = fprod([mpf('4'), pi, power(10, -7)])
>
> ?
>
> Thanks,
> -- Bernd
>
>
> ------------------------------
>
> Message: 5
> Date: Mon, 2 Feb 2009 14:46:18 -0800 (PST)
> From: Bernard Rankin <berankin99 at yahoo.com>
> Subject: [Tutor] regex: not start with FOO
> To: Tutor at python.org
> Message-ID: <528538.84097.qm at web112218.mail.gq1.yahoo.com>
> Content-Type: text/plain; charset=us-ascii
>
> Hello,
>
>
> I'd like to match any line that does not start with FOO. (Using just a
> reg-ex rule)
>
> 1) What is the effective difference between:
>
> (?!^FOO).*
>
> ^(?!FOO).*
>
> 2) Is there a better way to do this?
>
>
> Thanks,
> :)
>
>
>
>
>
>
>
> ------------------------------
>
> Message: 6
> Date: Mon, 02 Feb 2009 15:50:18 -0800
> From: "WM." <wferguson1 at socal.rr.com>
> Subject: [Tutor] newton's sqrt formula
> To: tutor at python.org
> Message-ID: <498786BA.6090002 at socal.rr.com>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> # program to find square root
> square = input ('Please enter a number to be rooted, ')
> square = square * 1.0
> guess = input('Please guess at the root, ')
> guess = guess * 1.0
> newguess = 0.
>
> while guess**2 != square:
> # Newton's formula
> newguess = guess - (guess * guess - square) / (guess * 2)
> guess = newguess
> guess**2 - square
> print
> print
> print guess, ' is the square root of ', square
> print
> print
> print 'bye'
> Last month there was a square root program discussed. I wondered if the
> tide of my ignorance had receded enough that I could take a whack at
> messing with it.
> I offer this rewrite for your critique. Can it be terser, faster, prettier?
> Thank you.
>
>
>
>
> ------------------------------
>
> Message: 7
> Date: Tue, 3 Feb 2009 00:44:27 -0000
> From: "Alan Gauld" <alan.gauld at btinternet.com>
> Subject: Re: [Tutor] newton's sqrt formula
> To: tutor at python.org
> Message-ID: <gm841b$l9t$1 at ger.gmane.org>
> Content-Type: text/plain; format=flowed; charset="iso-8859-1";
> reply-type=response
>
> "WM." <wferguson1 at socal.rr.com> wrote
>
> > square = input ('Please enter a number to be rooted, ')
> > square = square * 1.0
>
> Use raw_input() instead of input() and don't multiply
> by 1.0 - instead convert to float using float():
>
> square = float( raw_input ('Please enter a number to be rooted, '))
>
> > guess = input('Please guess at the root, ')
> > guess = guess * 1.0
> > newguess = 0.
> >
> > while guess**2 != square:
> > # Newton's formula
> > newguess = guess - (guess * guess - square) / (guess * 2)
> > guess = newguess
>
> You could just combine these two
>
> guess = guess - (guess * guess - square) / (guess * 2)
>
> > guess**2 - square
>
> That line does not do anything!
>
> > print
> > print
> > print guess, ' is the square root of ', square
> > print
> > print
> > print 'bye'
>
> Partly a style thing but I would prefer you either used triple
> quoted strings and format chars or inserted \n characters.
> ie either:
>
> > print """
>
>
> %s is the square root of %s
>
>
>
> bye""" % (guess, square)
>
> OR
>
> print "\n\n\n",guess," is the square root of", square,"\n\n\nbye!"
>
> Or combine both:
>
> print "\n\n\n%s is the square root of %s\n\n\nbye!" % (guess, square)
>
> Actually, if it was me I'd use two prints:
>
> print "\n\n\n%s is the square root of %s" % (guess, square)
> print"\n\n\nbye!"
>
> > I offer this rewrite for your critique. Can it be terser, faster,
> > prettier?
> > Thank you.
>
> Not very critical but maybe it helps...
>
> Alan G.
>
>
>
>
> ------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
> End of Tutor Digest, Vol 60, Issue 5
> ************************************
>
--
-------------------------------------------------
Visit Pipex Business: The homepage for UK Small Businesses
Go to http://www.pipex.co.uk/business-services
More information about the Tutor
mailing list