My code opens a file that contains a few lines of text. After, i make it a list using readlines()
and try to remove the "\n"
characters by using [e.strip() for e in list]
I've tried multiple approaches, searched the forum, etc. but when printing the list, the \n is always still present.
I didn't use map()
because I'm learning and this has to be done with the things I already know.
What I tried:
[e.strip() for e in gradesls]
for line in gradesls:
line = line.replace("\n", "")
for line in gradesls:
line = line.rstrip()
for line in gradesls:
line = line.rstrip("\n")
for line in gradesls:
line = line.strip("\n")
and so on.. nothing worked.
Code:
def main():
print("Choose a file you want chart-ified\n")
studFile = askopenfilename(filetypes=[("Text Document", ".txt"), ("Text
Document", ".doc"), ("Text Document", ".docx")])
grades = open(studFile, "r")
#get list of lines
gradesls = grades.readlines()
#remove newlines from list
for line in gradesls:
line = line.strip("\n")
print(gradesls)
What it prints out is always:
['6\n', 'Computewell 57\n', 'Dibblebit 90\n', 'Jones 34\n', 'Smith 68\n',
'Dorrer 88\n', 'Mayer 100']
instead of:
['6\n', 'Computewell 57\n', 'Dibblebit 90\n', 'Jones 34\n', 'Smith 68\n',
'Dorrer 88\n', 'Mayer 100']