Could not convert string to float

Hi! I have a “txt” file, which has several numbers like this:
1
2
3
4
I want my programme to sum all of them. I’ve done the following:
file=open(“n38.txt”, “r”)
result=0
for line in file:

n=float(line)
print(n)
result=result+n

print(“the sum is”, result)

But I get the mistake: "ValueError: could not convert string to float: ‘1\n’.
Where is the mistake? Is it in n=float(line)? Why? Thank you in advance

Maybe the file contains hidden characters…
Before casting line, print line with end character like dash “-” and post output.

Hello Izan.
So a file or file-like object in python is sort of like a buffer. In order to get data into the buffer or retrieve data from it, we have to write to it read from it respectively. So after opening you file. you had to call the read() method in order to retrieve its content.

Secondly, files and other objects like files require you to run a set of instructions in order to handle them properly. For example, it’s so easy for you to open a file just like you did and forget to close it later, also just like you did. In this case, we have a special handling mechanism called context managers and they are used with the with keyword in python, these help you run those instructions you’d usually forget and which might end up crushing you program.

So, this is how I’d have implemented what you wanted to do.

result = 0

file = open("n38.txt","r")
with file: # a context manager
    data = file.read() #this returns data as a string of numbers
    for num_char in data:
        result += float (num_char)

#file is automatically closed after
print ("the sum is: ",result)

I haven’t tested this out yet, but I’m sure it might work. So try that out, or perhaps take it as an example.

That code assigns every character in the file (one at a time) to num_char. It doesn’t split on newlines or read multi-digit numbers as a single number.

The error message in the first post shows that there are non-numeric characters in the first line before the “1”.

what I was going for is to read the whole file into one string of number character, then iterate the number characters in the number string one at a time while converting them to float and adding them to the result.
Since I’ve not particularly tested it, perhaps there’s an error. However, I’m sure it can be modified a little to allow for splitting around the new lines

result = 0

file = open("n28.txt","r")
with file: # a context manager
    for num_char in iter(file.readline,""): # iterate over the number characters in the file
        result += float(num_char)

#file is automatically closed after
print ("the sum is: ",result)

This only works if values are known and all convertible to floats. If conversion fails, perhaps it could be that the file was corrupted

Hi Izan.

Read the error message. It tells you exactly what is wrong. Your file

does not contain only numbers. The first line is supposed to be “1\n”

but is actually “1\n”.

(Remember the \n means a newline.)

How did you create the file? Did you use Notepad?

You can fix this by telling Python to open this as a UTF-8 file with a

fake “BOM” (Byte-Order-Mark) which is something that Notepad uses.

file = open("n38.txt", "r", encoding="utf-8-sig")

Try that and report if it fixes the problem.

Bloody hell, I am getting sick and tired of the number of bugs in the

Discuss software.

When I wrote this earlier:

The first line is supposed to be “1\n” but is actually “1\n”.

it is not because I’m mad or an idiot. It is because the second string

included three extra characters when I sent it, which the Discuss

software deleted from the emails.

The three characters were:

  • LATIN SMALL LETTER I WITH DIAERESIS

  • RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK

  • INVERTED QUESTION MARK

followed by the 1 and newline.

So here is an experiment to see if I can replicate the bug. The

following two bracketed strings contain the same three characters, the

first set separated by dashes, the second without:

(ï–»–¿)

()

My prediction is the first one, with the dashes, will appear, and the

second will not.

Well, so much for my prediction. I give up, I cannot predict the

behaviour of Discuss with emailed text.

The problem was the txt file and rstrip, which I did not include. I created the file using Google Docs and dowloaded as txt. I do not know why this txt file does not work. So, I created a new file using Notepad. My solution:
file=open(“n38bn.txt”, “r”)
result=0
for line in file:

line1=line.rstrip()
n=int(line1)
print(n)
result=result+n

print(“the sum is”, result)

In general, as you’ve learned, its a bad idea to use rich text editors (Like Google Docs, MS Word and LibreOffice Writer) to create plain text files. Often, they will muck around and do things you don’t want. Rather, you should use a text editor, preferably a real text editor, like Notepad++, Sublime Text or VSCode, that doesn’t have the numerous limitations and issues that the built-in Notepad does.

To note, your solution converts the number to an int, not a float, as your initial problem statement specified. Furthermore, please carefully read what others have written about the other potential issues with this implementation, including not using a context manager (with statement) to ensure the file is actually closed, making sure to specify the file encoding, and others. You can also be much more concise, and replace that with, for example:

with open("n38bn.txt", r, encoding="utf-8") as file:  # Open the file for the length of the width block
    numbers = [float(line.strip()) for line in file]  # For each line, strip, convert to float and add to list
result = sum(numbers)  # Sum the list
print("The sum is", result)

This uses a list comprehension to build the list in one line, and avoids other identified issues with the previous solution (leaving the file open, not specifying the encoding, not converting to float, etc).

You can also use IDLE’s text editor to create plain text files.

For development, it can be easier to start with data in the code file as a string.

data = """\
 1
41
20
"""
sum = 0
for line in data.splitlines(keepends=True):  # Keep '\n' as when reading file by lines.
    sum += int(line.strip())
print(sum)  # 62

When this works, then add with open(...) as data above for line... and indent the for statement.

1 Like