[Tutor] Defining Exceptions

davidheiserca at gmail.com davidheiserca at gmail.com
Wed Feb 2 19:44:23 CET 2011


It's difficult to see exactly what the data looks like as it is received. Where are the line breaks? At the commas?

Is the "header" just the first line? Is "1Time" considered part of the header? Is everything after "1Time" considered "data"?

I can see several simple alternatives to your method. I don't think you need the exception code.


I think you can replace this:

    lines = resp.split('\r\n')
    waste=lines.pop()
    while True:
        try:
            lines.remove(waste)
        except Exception:
            ??????

with this:

    lines = resp.splitlines[:2]



This part, I don't understand:

    for x,row in enumerate(lines):
        lines[x]=row.split(',')




  ----- Original Message ----- 
  From: Tom Brauch 
  To: tutor at python.org 
  Sent: Wednesday, February 02, 2011 6:51 AM
  Subject: [Tutor] Defining Exceptions


  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()
      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



------------------------------------------------------------------------------


  _______________________________________________
  Tutor maillist  -  Tutor at python.org
  To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110202/4e919ed2/attachment-0001.html>


More information about the Tutor mailing list