2

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?

Karnivaurus
  • 22,823
  • 57
  • 147
  • 247

7 Answers7

4

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
Community
  • 1
  • 1
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125
3

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

gmetaxo
  • 3
  • 3
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
1

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.

1

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.

maxadorable
  • 1,290
  • 1
  • 10
  • 24
0

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))
0

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()
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
0

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
abu8na9
  • 103
  • 1
  • 8