Skip to content

Python script to format the TDS B series CSV files.

In my last post I griped about the Tektronix TDS2004B spitting out horrible CSV files. I wrote this script to reformat it. As you can see from the source, whatever is in the “header” file just gets dumped ahead of the data. I just checked it and it should work with 1, 2, 3, or 4 channel files.

  1. #Formats the horrendous TDS****B CSV data files into a format
  2. #compatible with GNUplot
  3. import sys, string
  4.  
  5. if len(sys.argv) <> 4:
  6.     msg = ‘The header file is simply prepended to the data.’
  7.     sys.exit(‘nnnUsage:TDSformat.py <csv>  <out>.n%snnn’ % msg)
  8.  
  9. CSV =       open(sys.argv[1], ‘r’)
  10. header =    open(sys.argv[2], ‘r’)
  11. outfile =   open(sys.argv[3], ‘w’)
  12.  
  13. for line in header:
  14.     outfile.write(line)
  15.  
  16. header.close()
  17.  
  18. outfile.write(‘n’)
  19.  
  20. for line in CSV:
  21.     cols = string.split(line, ‘,’)
  22.  
  23.     #First, spit out the lines as they are.
  24.  
  25.     if len(cols) == 24:      #4 channels
  26.         print ‘%st%st%st%st%s’ % (
  27.             cols[3], cols[4], cols[10], cols[16], cols[22] )
  28.     elif len(cols) == 18:    #3 channels
  29.         print ‘%st%st%st%s’ % (
  30.             cols[3], cols[4], cols[10], cols[16] )
  31.     elif len(cols) == 12:    #2 channels
  32.         print ‘%st%st%s’ % (
  33.             cols[3], cols[4], cols[10] )
  34.     elif len(cols) == 6:     #1 channel
  35.         print ‘%st%s’ % (
  36.             cols[3], cols[4] )
  37.  
  38.     #Now change the "." to 0’s.
  39.  
  40.     if cols[4] == ‘.’:
  41.         cols[4] = 0
  42.     if len(cols) > 6 and cols[10] == ‘.’:
  43.         cols[10] = 0
  44.     if len(cols) > 12 and cols[16] == ‘.’:
  45.         cols[16] = 0
  46.     if len(cols) > 18 and cols[22] == ‘.’:
  47.         cols[22] = 0
  48.  
  49.     cols[3] =  float(cols[3])
  50.     cols[4] =  float(cols[4])
  51.     if len(cols) > 6:
  52.         cols[10] = float(cols[10])
  53.     if len(cols) > 12:
  54.         cols[16] = float(cols[16])
  55.     if len(cols) > 18:
  56.         cols[22] = float(cols[22])
  57.  
  58.     #Write the data in the new file.
  59.  
  60.     outfile.write(‘%et%e’ % ( cols[3], cols[4] ))
  61.     if len(cols) > 6:
  62.         outfile.write(‘t%e’ % ( cols[10] ))
  63.     if len(cols) > 12:
  64.         outfile.write(‘t%e’ % ( cols[16] ))
  65.     if len(cols) > 18:
  66.         outfile.write(‘t%e’ % ( cols[22] ))
  67.  
  68.     outfile.write(‘n’)
  69.  
  70. CSV.close()
  71. outfile.close()

This should get you started.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*