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
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
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:
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.