python - Problems removing \n from a list -
this question has answer here:
- how remove \n list element? 13 answers
i have looked various solutions make sense, , tried implement them in code. when print out list i'm still receiving number newline character. here code:
infile = open(r"c:\users\rr205951\documents\nums.txt", "r") numlist = infile.readlines() print("before: ", numlist) num in numlist: num = num.rstrip('\n') print("after: ", numlist)
when run above code still returns following:
before: ['15\n', '4\n', '3\n', '25\n', '2000\n', '328\n', '20\n', '9\n', '18\n', '4333\n'] after: ['15\n', '4\n', '3\n', '25\n', '2000\n', '328\n', '20\n', '9\n', '18\n', '4333\n']
you're not modifying numlist. one-liner list comprehension so:
#read lines did, add following after.
numlist = [num.strip() num in numlist]
Comments
Post a Comment