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
_______________________________________________
Portland mailing list
[email protected]
http://mail.python.org/mailman/listinfo/portland

Reply via email to