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
How about this approach:
x = []
with open('foo.dat', 'r') as f:
for line in f:
if line: #avoid blank lines
x.append(float(line.strip()))
Or:
with open('foo.dat', 'r') as f:
lines = (line.strip() for line in f if line)
x = [float(line) for line in lines]
Finally more compact:
with open('foo.dat', 'r') as f:
x = [float(line.strip()) for line in f if line]
This way you don't have to worry about blank lines and you make proper conversion from string to float
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With