I have a text file called "foo.txt", with a list of numbers, one on each line, for example:
0.094195
0.216867
0.326396
0.525739
0.592552
0.600219
0.637459
0.642935
0.662651
0.657174
0.683461
I now want to read these numbers into a Python list. My code to do this is as follows:
x = []
file_in = open('foo.dat', 'r')
for y in file_in.read().split('\n'):
x.append(float(y))
But this gives me the error:
ValueError: could not convert string to float
What am I doing wrong?
Edit:
commented by martineau:
you can also use if y:
to eliminate None
or empty string.
Original Answer:
It fails due to you are using newline character as a separator, therefore the last element is empty string
you can add y.isdigit()
to check whether y is numeric.
x = []
file_in = open('sample.csv', 'r')
for y in file_in.read().split('\n'):
if y.isdigit():
x.append(float(y))
OR
you can change read().split("\n")
to readlines()
OR
remove the leading/trailing characters from y. it handles the lines with extra whitespaces
for y in file_in:
trimed_line = y.strip() # leading or trailing characters are removed