Reputation: 307
I am using the following code to extract text from .txt file into a list
with open("german.txt", "r") as f:
stopwords = []
for item in f:
# print(item)
stopwords.append(item)
print(stopwords)
sample of the german.txt file
aber
alle
allem
allen
aller
alles
als
the results
['aber\n', 'alle\n', 'allem\n', 'allen\n'......
Why am I getting \n with each item? Is there an easy way to complete this without the \n
Upvotes: 1
Views: 677
Reputation: 993105
You can use:
stopwords.append(item.rstrip())
The rstrip()
method:
Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped:
Upvotes: 2