I'm trying to extract anomalous data points from a large csv file (~1e6 lines) in which most of the data points are at constant value. I've written the code below to detect values lower than the constant.
constant = 1
try:
fp = open('disk2.csv')
for line in fp:
ch4 = float(line.split(",")[4]) #data from channel four is in the fifth column
if ch4 < constant:
print line.split(",")[0] #print first column
except:
ch4 = 'Not found'
finally:
fp.close()
print(ch4,type(ch4))
the print returns the following, without additional errors:
('Not found', <type 'str'>)
if I change the code to:
constant = 1
try:
fp = open('disk2.csv')
for line in fp:
ch4 = line.split(",")[4] #data from channel four is in the fifth column
if ch4 < constant:
print line.split(",")[0] #print first column
except:
ch4 = 'Not found'
finally:
fp.close()
print(ch4,type(ch4))
It returns
(' 2.41650E+01', <type 'str'>)
So, the csv file is read as a string, and the string can be divided into a list using the split command, but I cannot turn the items in the list into floating numbers?
float_value = float(ch4)