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.
-
#Formats the horrendous TDS****B CSV data files into a format
-
#compatible with GNUplot
-
import sys, string
-
-
if len(sys.argv) <> 4:
-
msg = ‘The header file is simply prepended to the data.’
-
sys.exit(‘nnnUsage:TDSformat.py <csv> <out>.n%snnn’ % msg)
-
-
CSV = open(sys.argv[1], ‘r’)
-
header = open(sys.argv[2], ‘r’)
-
outfile = open(sys.argv[3], ‘w’)
-
-
for line in header:
-
outfile.write(line)
-
-
header.close()
-
-
outfile.write(‘n’)
-
-
for line in CSV:
-
cols = string.split(line, ‘,’)
-
-
#First, spit out the lines as they are.
-
-
if len(cols) == 24: #4 channels
-
print ‘%st%st%st%st%s’ % (
-
cols[3], cols[4], cols[10], cols[16], cols[22] )
-
elif len(cols) == 18: #3 channels
-
print ‘%st%st%st%s’ % (
-
cols[3], cols[4], cols[10], cols[16] )
-
elif len(cols) == 12: #2 channels
-
print ‘%st%st%s’ % (
-
cols[3], cols[4], cols[10] )
-
elif len(cols) == 6: #1 channel
-
print ‘%st%s’ % (
-
cols[3], cols[4] )
-
-
#Now change the "." to 0’s.
-
-
if cols[4] == ‘.’:
-
cols[4] = 0
-
if len(cols) > 6 and cols[10] == ‘.’:
-
cols[10] = 0
-
if len(cols) > 12 and cols[16] == ‘.’:
-
cols[16] = 0
-
if len(cols) > 18 and cols[22] == ‘.’:
-
cols[22] = 0
-
-
cols[3] = float(cols[3])
-
cols[4] = float(cols[4])
-
if len(cols) > 6:
-
cols[10] = float(cols[10])
-
if len(cols) > 12:
-
cols[16] = float(cols[16])
-
if len(cols) > 18:
-
cols[22] = float(cols[22])
-
-
#Write the data in the new file.
-
-
outfile.write(‘%et%e’ % ( cols[3], cols[4] ))
-
if len(cols) > 6:
-
outfile.write(‘t%e’ % ( cols[10] ))
-
if len(cols) > 12:
-
outfile.write(‘t%e’ % ( cols[16] ))
-
if len(cols) > 18:
-
outfile.write(‘t%e’ % ( cols[22] ))
-
-
outfile.write(‘n’)
-
-
CSV.close()
-
outfile.close()
This should get you started.
Post a Comment