newline character problems with Python (strip)

If I write

f = open("file.csv","r")
    if f.mode == "r":
        cols = []
        snames = []
        svals = []
        rows = f.readlines()
        for x in rows:
            x.strip("\n")
            cols = x.split(",")
            snames.append(cols[0])
            svals.append(cols[1])

then svals still has the \n characters I dont want e.g. '20.43256639\n'

but if I do this instead, then svals shows the correct values without the \n

for x in rows:
    cols = x.split(",")
    snames.append(cols[0])
    svals.append(cols[1].strip("\n"))

svals DOES have the right values e.g. '20.43256639'



Read more here: https://stackoverflow.com/questions/64893338/newline-character-problems-with-python-strip

Content Attribution

This content was originally published by DM71 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: