I am iterating through my text file, but when I use the read() function, the loop iterates through the letters instead of the sentences.
with the following code:
for question in questions: # voor elke question moet er door alle lines geiterate worden
print(f"Question: {question}")
f = open("glad.txt", "r")
text = f.read()
# text = text.replace("\n", ". ")
# text = text.replace(". .", "")
# text = text.replace(".. ", ". ")
# text = text.replace(".", ".\n")
#text = text.strip(".. ")
# test = text.replace('[bewerken | brontekst bewerken]', "")
# output = re.sub(r'\[\d+\]', '', test)
for line in text:
text = str(line) #het antwoord moet een string zijn
#encoding met tokenizen van de zinnen
print(text)
The output is:
But when I remove the f.read()
I receive the expected out:
I need to use the read() function, otherwise I cannot use the replace() function. Does anyone how to solve this issue?
To do that, first, open the file using Python open() function in read mode. The open() function will return a file handler. Use the file handler inside your for-loop and read all the lines from the given file line by line. Once done,close the file handler using close() function.
To iterate through lines in a file using Python, you can loop over each line in a file with a simple for loop. When reading files, the ability to read files sequentially line by line can be very useful. Reading text from a file is easy with the Python open() function.
In Python, there is not C like syntax for(i=0; i<n; i++) but you use for in n . They can be used to iterate over a sequence of a list , string , tuple , set , array , data frame . Given a list of elements, for loop can be used to iterate over each item in that list and execute it.
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string). This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.
f.read()
converts the text file into a string. So if you iterate through a string, it will loop every character:
>>> for i in 'Hello World':
print(i)
H
e
l
l
o
W
o
r
l
d
>>>
Whereas without .read()
, just f
, would iterate line by line through the text file, it would be a sequence of strings (each line), it would be something like:
>>> for i in ['Hello', 'World']:
print(i)
Hello
World
>>>
Using text = f.read()
, you are getting the whole text file into text
. When you iterate over a string in Python, it gives you one character per iteration.
Since you want to continue using .read()
, use splitlines()
:
text = f.read().splitlines()
Now, text
is a list which you can freely iterate the same way you are already doing:
for line in text:
Other answers described the problem. However, the standard way to do this in python according to the best practices would be the following:
with open("glad.txt", "r") as file:
for line in file:
print(line)
# do the stuff
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With