[portland] Reformatting Data Files
Ethan Furman
ethan at stoneleaf.us
Tue Mar 22 21:37:49 CET 2011
Rich Shepard wrote:
> This has worked flawlessly for some of the exported spreadsheet pages.
> Now, however, I have some pages that include the units for the measured
> parameters (e.g., mg/L) which is the third-to-last column in the postgres
> table.
>
[snip]
> outdata.writerow([location, sampdate[i], row[0], float(row[i])])
How about a function that either floats your string, or returns the
string if it's not a number?
Something like this:
--> def flt_or_str(unk):
... try:
... return float(unk)
... except ValueError:
... return unk
...
--> float("1")
1.0
--> float("hello")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): hello
--> flt_or_str("1")
1.0
--> flt_or_str("829mg/L")
'829mg/L'
Then your line of codes becomes:
outdata.writerow([location, sampdate[i], row[0], flt_or_str(row[i])])
Hope this helps!
~Ethan~
More information about the Portland
mailing list