[portland] Reformatting Data Files
Rich Shepard
rshepard at appl-ecosys.com
Tue Mar 22 22:19:55 CET 2011
On Tue, 22 Mar 2011, Ethan Furman wrote:
> How about a function that either floats your string, or returns the string if
> it's not a number?
Ethan,
Thank you. I'm moving among emacs, SQL, R, and python and think my brain
short-circuited along the way. Didn't think of this.
> 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!
Certainly does.
Much appreciated,
Rich
More information about the Portland
mailing list