Home > Mobile >  Copying values from text file to a list
Copying values from text file to a list

Time:11-03

When I copy some values from text file to a list a "\n" is also being printed. How can I remove that. I tried split(), replace(), remove() and so many ways but didn't work.

I am expecting to remove the "\n" when values are copied from text file to list

CodePudding user response:

I cannot comment, but You need to use strip()

CodePudding user response:

Try This!

file = open('textfile.txt', 'r')
Lines = file.readlines()

wordlist = []

for line in Lines:
    word = (line.split()[0])
    wordlist.append(word)
        
  • Related