[Tutor] Defining Exceptions

Emile van Sebille emile at fenx.com
Thu Feb 3 15:44:07 CET 2011


Hi Tom,

First, you sent this only to me -- be sure to reply all so that the 
group can participate.

On 2/2/2011 10:25 AM Tom Brauch said...
 > Thanks, Emile.
 >
 > If I replace the question marks in my script with break it throws out
 > everything before the break (<) and gives me the header starting at 4 -
 > Display.
 >
 > Knowing that it works to that point I was hoping to be able to tell the
 > script to throw out everything before the "T," at the end of the 
header in
 > which case I get only the data.
 >
 > How do I define "T," so that python recognizes it?  Is it an iteration?


You need to understand the structures you're working with.  lines is a 
list that you created with "lines = resp.split('\r\n')" -- add a line 
immediately after this that reads : "for ii in lines: print ii" (or 
print(ii) if you're on v3+  are you?) -- that'll show you what you've 
got.  Paste the results in your reply.  If the line you're interested in 
literally is the only one that has ',T,' in it, you could keep the part 
after that more easily with something like:

...
     ser.write('3')
     resp=buffer_read(ser)
     waste,goodlines = resp.split(",T,")
...

and go from there using goodlines.


HTH,

Emile



 >
 > Sorry I can't ask more intelligent questions but I'm truly just getting
 > started.
 >
 > Thanks again!
 > Tom
 >
 >
 > On Wed, Feb 2, 2011 at 10:58 AM, Emile van Sebille <emile at fenx.com> 
wrote:
 >
 >> On 2/2/2011 6:51 AM Tom Brauch said...
 >>
 >>  All,
 >>>
 >>> I am a python neophyte and not terrible well versed in programming (as
 >>> will
 >>> become obvious shortly)
 >>>
 >>> I have a script which is reading a serial device on a schedule.  The
 >>> device
 >>> outputs a header at the beginning of every read.  I have a data 
file which
 >>> I
 >>> am appending and would like to eliminate the header so that I end 
up with
 >>> one master data file.  A copy of the serial readout is:
 >>>
 >>> *
 >>>
 >>>>
 >>>>  6CSV Type Reports2 - Display All Data3 - Display New Data4 - Display
 >>> Last
 >>> Data5 - Display All Flow Stats6 - Display New Flow Stats7 - Display All
 >>> 5-Min Flow8 - Display New 5-Min Flow9 - Display Error Log>
 >>> 4 - Display CSV DataStation,
 >>>
 >>> 
1Time,Conc(mg/m3),Qtot(m3),no(V),WS(MPS),no(V),RH(%),no(V),AT(C),E,U,M,I,L,R,N,F,P,D,C,T,02/02/11
 >>> 08:00,  0.042, 0.701, 0.004,   0.1, 0.002,     7, 0.012,
 >>> -18.0,0,0,0,0,0,0,0,0,0,0,0,0,
 >>>
 >>> I am only interested in the information following the T, in the 
header.  I
 >>> have tried to use an exception to have the script throw out all 
data prior
 >>> to the T, in the header but I don't know how to define this excecption.
 >>>  My
 >>> current script looks as follows:
 >>>
 >>> def cycle(ser,prefix):
 >>>     inRecovery=False
 >>>     resp=False
 >>> #        tm.sleep(30.0)
 >>>     ser.open()
 >>>     tm.sleep(2)
 >>>     ser.write('\n\r\n\r\n\r')
 >>>     resp=buffer_read(ser)
 >>>
 >>>     ser.write('6')
 >>>     resp=buffer_read(ser)
 >>>
 >>>     ser.write('3')
 >>>     resp=buffer_read(ser)
 >>>     lines = resp.split('\r\n')
 >>>     waste=lines.pop()
 >>>
 >>
 >> pop changes lines by discarding the end of the list.  So your subsequent
 >> remove fails because waste is no longer in the list.
 >>
 >> Perhaps waste is the data you're looking for?
 >>
 >> sprinkle some prints through here so you can see what values you're 
dealing
 >> with.  That's a common technique to help figure out what's wrong.
 >>
 >> HTH,
 >>
 >> Emile
 >>
 >>
 >>      while True:
 >>>         try:
 >>>             lines.remove(waste)
 >>>         except Exception:
 >>>             ??????
 >>>     for x,row in enumerate(lines):
 >>>         lines[x]=row.split(',')
 >>>     if DEBUG: print lines
 >>>     f=open(prefix,'a')
 >>>     csvf = csv.writer(f)
 >>>     csvf.writerows(lines)
 >>>     f.close()
 >>>     ser.close()
 >>>
 >>> How do I define the exception so that I get only the data following the
 >>> header?
 >>>
 >>> Thanks!
 >>> Tomb
 >>>
 >>>



More information about the Tutor mailing list