[portland] Reformatting Data Files
Rich Shepard
rshepard at appl-ecosys.com
Wed Feb 2 18:55:33 CET 2011
My question is why the output file, out.csv, displays in emacs as having
DOS end-of-lines instead of the normal UNIX \n? Here is the script that I'm
using; it's essentially what Kyle threw together:
---------------------
#!/usr/bin/env python
import sys,csv
filename = sys.argv[1]
try:
infile = open(filename, 'r')
except:
print "Can't open ", filename,"!"
sys.exit(1)
indata = csv.reader(infile, delimiter=':', quotechar="'")
loc = indata.next()
sampdate = indata.next()
# check for mis-match in number of columns
assert len(loc) == len(sampdate)
outfile = open('out.csv', 'w')
outdata = csv.writer(outfile, quoting = csv.QUOTE_NONNUMERIC, quotechar = "'", delimiter = ':')
# The next rows are our parameters.
for row in indata:
# iterate through each column for the particular locations
for i, location in enumerate(loc):
# ignore the first column, which contains the parameter name
if i == 0:
continue
# Write the location, the date for this column, the parameter name
# (which is the first column in this row), and the value.
if row[i] != '':
float(row[i])
outdata.writerow([location, sampdate[i], row[0], float(row[i])])
else:
row[i] = ''
outdata.writerow([location, sampdate[i], row[0]])
infile.close()
outfile.close()
-----------------
Rich
More information about the Portland
mailing list