0

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']
rawwar
  • 4,834
  • 9
  • 32
  • 57
Kevin
  • 101
  • 1
  • 2
  • Strings are immutable; so when you're doing `line = line.strip()`, the stripped line isn't actually changing in `gradesls`. But maybe this is not the issue, since you've tried a list comprhension. Have you tried `gradesls = [line.strip() for line in gradesls]`? – Anakhand Jan 29 '19 at 11:25
  • You need to assign it back: `gradesls = [e.strip() for e in gradesls]` - these are not mutating operations for in-place mutation - they return a mutated new list – Patrick Artner Jan 29 '19 at 11:26
  • Doesn't the list comprehension basically do the same thing as my for loops? (Haven't really dove into list comprehensions yet). Why would a string be mutable when using a list comprehension? – Kevin Jan 29 '19 at 11:26
  • @PatrickArtner that worked, thanks! I wonder why that same command dosn't work in a for-loop though. – Kevin Jan 29 '19 at 11:28
  • It does not work - because it modifies `line` ... not the content of the list. You can do `gradesls = [x.strip() for x in grades.readlines()]` directly. – Patrick Artner Jan 29 '19 at 11:29
  • 1
    @Kevin The reason it doesn't work in a `for` loop is because strings are immutable in python, so you can't "edit" a string in place. You have to make an "edited copy" an then reassign it to the previous identifier. When the interpreter reaches `line = line.strip()` it rebinds the _local_ `line` identifier to `line.strip()`, but it doesn't reassign `gradesls[i]` – Anakhand Jan 29 '19 at 11:30
  • That makes sense, thanks for the input guys – Kevin Jan 29 '19 at 11:44
  • Use following code to read file: `lines = open(filename).read().splitlines()` bydefault it removes `\n` – Anonymous Jan 29 '19 at 12:08

0 Answers0