Re: Reading a tab delimited text file.



Stephen wrote:

Hi,

I would like to read a text file of numbers produced by a data
acquisition system into three vectors of doubles. The contents of the
file are:

+0.0000000e+0 +2.7645134e+1 +2.7745625e+1

+0.4100041e-1 +2.7637787e+1 +2.7731047e+1

+0.0820008e+0 +2.7645134e+1 +2.7750483e+1
...

or

+0.0000000e+0\t+2.7645134e+1\t+2.7745625e+1\r\n
...

I would like to read the first column into time_vec, second into
ch1_vec and so on.

(I have never programmed in python and am having trouble finding a way
to do this).

A simple starting point, without error handling, etc., could be:

a_list = []
b_list = []
c_list = []
for line in open ('my_numbers.txt', 'rt'):
a, b, c = [float (x) for x in line.split()]
a_list.append (a)
b_list.append (b)
c_list.append (c)

Assuming every line has three tab-separated numbers, the text versions of
the numbers are all convertable to float, the text file that's opened will
eventually be closed (whether by garbage collection or program
termination.)

This code does illustrate the string's split method, list comprehension,
sequence unpacking, and the fact that each of a_list, b_list, c_list *MUST*
be initialized with a distinct empty list object.

Mel.


.



Relevant Pages

  • Re: using MFC VC++ - which is more efficient - float or double?
    ... I'm about to say may be null and void. ... I don't think its true that the hardware does everything as doubles. ... there's nothing that is 'saved' by using float. ... If you are working with large sets of data, then memory. ...
    (microsoft.public.vc.mfc)
  • Re: using MFC VC++ - which is more efficient - float or double?
    ... I'm about to say may be null and void. ... As far as I know everything is floating point and doubles takes extra processing to emulate. ... there's nothing that is 'saved' by using float. ... This gets to be a problem sometimes even with a Gig or several Gigs of memory. ...
    (microsoft.public.vc.mfc)
  • Re: using MFC VC++ - which is more efficient - float or double?
    ... Measurements made in debug mode are usually useless. ... float f2 = 5.0; ... __declspecvoid DoubleComputation() ... I don't think its true that the hardware does everything as doubles. ...
    (microsoft.public.vc.mfc)
  • Re: using MFC VC++ - which is more efficient - float or double?
    ... float f2 = 5.0; ... int I = 5000000; ... __declspecvoid DoubleComputation() ... As far as I know everything is floating point and doubles takes extra processing to emulate. ...
    (microsoft.public.vc.mfc)
  • Re: why float
    ... A double *is* a float with more precision. ... It is possibly to do with spurious precision in the doubles - in numerical ... I think it depends on teh algorithm as well. ... single-precision in order to stop rounding errors from amplifying ...
    (microsoft.public.dotnet.languages.csharp)