I have a text file named datos.txt
with numeric values, like so:
2
5
1
I read it with the following code:
datos = []
with open ("datos.txt") as fichero:
for dato in fichero.read():
datos.append(dato)
This reads each line as a string. Then I try to convert each element of the data list to numeric, like this:
for dato in datos:
numero = int(dato)
print (numero)
This should work, but I get the following error:
ValueError: invalid literal for int() with base 10: '2\n3'
Why does that error occur? How can I convert that data which is str
to int
? I am using the Python 3 IDLE
The problem is that it
fichero.read()
reads the entire file in a single text string. So when you dofor dato in fichero.read()
you will be iterating through that string.Iterating through a string causes it to be processed one character at a time. That is generally wrong. Imagine that in a line you have the data "25". When processing it character by character you would be considering it as two data, the "2" and the "5". In addition you have the problem that certain characters are not digits, but the line endings (
\n
). An alternative is the functionreadlines()
. This function returns a list of strings, each corresponding to one line of the file.That way each iteration of the loop returns you a complete line (from which you have removed the one
\n
at the end). That line will already contain one of the data that you can convert toint
.You can take advantage of converting to integer already directly in each iteration. That way the list
datos
will already be integers instead of strings.More "pythonic" forms
The variable itself
fichero
is iterable in Python, and in each iteration it returns a line, so we could put directly:This is preferable to using
readlines()
since the list of lines created byreadlines()
will be taking up memory (more the larger the file read). On the other hand, iterating through the file reads the lines one at a time without saving them in any list.Using list comprehension the above code can be reduced a bit more: