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?

4
  • 2
    Is there a blank line at the end of your file? Commented Mar 31, 2016 at 20:59
  • 2
    You should print(y) before your x.append(float(y)) line to see what value is failing to be converted. Commented Mar 31, 2016 at 21:00
  • Why do you split('\n')? ... let it split() Commented Mar 31, 2016 at 21:09
  • Please do not use the anti pattern of for y in file_in.read().split('\n'): Just do for y in file_in: to iterate over a text file line-by-line. Commented Mar 31, 2016 at 21:21

7 Answers 7

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
Sign up to request clarification or add additional context in comments.

2 Comments

Could also say if y:, append(float(y)).
@martineau cannot agree more! eliminate None or empty string
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

Comments

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.

Comments

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.

Comments

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.