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:
            cols = x.split(",")

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(",")

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

Read more here:

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: