[Tutor] How best to create a sharp telemetry log file - from
one long string of teletry?
alan.gauld@bt.com
alan.gauld@bt.com
Fri Jan 24 06:22:42 2003
Hi Bill,
> Now I'm trying to write a nicely formatted log file out of a single
> string of telemtry that I read of a serial line.
> The string had 40 different values - numbers such a "120", and titles
> sucs as "OFT" There are 40 of these seperated by white space.
You don't say whether these come in a predetermined sequence that is
repeatable.
If it is a fixed set then you can use syring slicing to extract the digits:
field1 = strng[4:9] #or whateve.
Beterr to define these external to the slicing:
# field name = (start,length)
field1 = (4,5)
field2 = (13,3)
etc...
Then you can slice like:
f1 = strng[field1[0]:field1[0]+field1[1]]
f1 = int(f1) # if you need them as numbers rather than numeric strings
etc
> place each numerical value into it's own column?
>
> I've learned about the string format codes from Alan's book (page 32)
Thats the best solution, just define the line format in terms of field
specifiers:
fmt = "%12s%3d%7d%7s%8.2f\n"
Then in a loop create each line
output.write(headingStrings) # report header
for lines in buffer:
getFields() # assume uses slicing as above
result = fmt % (f1,f2,f3....)
output.write(result)
> say for the value of 120.
>
> '%8.2f' % 120
This will print
" 120.00"
Is that what you want? If its not a floating pint value a %8d might
be better.
Finally if the string is non deterministic in its numeric content
you might be better using a regular expression and a findall()
call to get a list of the strings.
Alan g.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld/