slicing the end of a string in a list
John Salerno
johnjsal at NOSPAMgmail.com
Thu Mar 2 22:13:11 EST 2006
Here's the code I wrote:
file = open('C:\switches.txt', 'r')
switches = file.readlines()
i = 0
for line in switches:
line = switches[i][:-1]
i += 1
print switches
You can probably tell what I'm doing. Read a list of lines from a file,
and then I want to slice off the '\n' character from each line. But
after this code runs, the \n is still there. I thought it might have
something to do with the fact that strings are immutable, but a test
such as:
switches[0][:-1]
does slice off the \n character. So I guess the problem lies in the
assignment or somewhere in there.
Also, is this the best way to index the list?
More information about the Python-list
mailing list