Reputation: 24121
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?
Upvotes: 2
Views: 55206
Reputation: 10951
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
Upvotes: 3
Reputation: 103
You can use this way
Note : reedline() is a string, reedlines() is a list
file_in = open('foo.dat', "r")
r = file_in.readlines()
file_in.close()
l = 0
x = []
while l < len(r) :
floating = float(r[l])
x.append(floating)
l += 1
Upvotes: 0
Reputation: 6110
You can use a function to decide whether the string you're parsing is a number or not. Based on this question 1 you can do that this way:
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
x = []
file_in = open('filename.dat', 'r')
for y in file_in.read().split('\n'):
if is_number(y):
x.append(float(y))
file_in.close()
Upvotes: 0
Reputation: 31895
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
Upvotes: 4
Reputation:
I think You string like this : '0.1111, 0.1111' or other
file_in = open('foo.dat', 'r')
for y in file_in.readlines()[0]:
x.append(float(y))
Upvotes: 0
Reputation: 1284
You can try using the default float function
>>> float("1.1")
1.1
You could also try using the python try else statement which will run the code until it catches a error and runs a else statement.
try:
try_this(whatever that might bring up a error)
except SomeException as exception:
#Handle exception
else:
return something
There might be a possibility that there is a blank line end of the file which might create errors. Try using the try else statement because of it.
Upvotes: 1
Reputation: 61
Usually files has empty line at the end so probably you're trying to cast empty string to float.
Instead of file_in.read().split('\n')
you could use:
for line in file_in.readlines():
x.append(float(line))
Method readlines
returns list of all lines from given file and skips last empty line if present.
Upvotes: 1