Casting str in Scientific Notation to Float fails only on Positive numbers in Python -
i reading in couple of columns text file (delimited 3 spaces reason)
the columns in scientific notation. first column has mixture of positive , negative numbers.
when casting float in segment:
count = 0 curfile = open(curfile, "r") row in curfile.readlines(): if count > (row_first-1) , count < row_last: line = row.split(' ') x.append(float(line[0])) print line[0] y.append(float(line[1])) count = count + 1 else: count = count + 1
it fails when switching negative row positive row first column
-3.0000000e+00 5.5100000e+02 2.6600000e+02 0.0000000e+00 -1.0000000e+00 5.5100000e+02 2.6600000e+02 0.0000000e+00 1.0000000e+00 5.5100000e+02 2.6600000e+02 0.0000000e+00
so in case convert -1(the second row) not 1(the third row). when appending values y positive, problem not occur.
i thought maybe there blank space before positive numbers "-" sign using lstrip() on string did not help.
i totally perplexed issue , appreciate ideas.
edit: exception gets thrown when run program:
traceback (most recent call last): file "u:\scripts\flow3d_script\flow_3d.py", line 93, in <module> x.append(float(line[0])) valueerror: not convert string float:
this guess, but...
could there 3 spaces @ start of negative lines too? cause split
return list empty string @ start.
to resolve should lstrip()
entire line before splitting. , way, if split()
no arguments defaults "split number of spaces", don't have worry number of spaces.
Comments
Post a Comment