python - Problems removing \n from a list -


this question has answer here:

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

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -