<etrade.griffi...@dsl.pipex.com> wrote>
 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?

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?

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.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to