Vimos
Vimos

Reputation: 701

python strip() in reading lines from a file

when i try to use strip() in the following condition, the output is a little different.

for line in tagfile:
    tag_name = line.strip("<>")
    print tag_name

the output is strike>

but if I use the following method

 tag_name = "<strike>"
 print tag_name.strip("<>")

the output is strike

Anybody who can help this?

Upvotes: 1

Views: 788

Answers (1)

perreal
perreal

Reputation: 98088

It is due to the newline at the end of the line, strip does not go beyond it since you are not specifying newline as a token. Try this:

for line in tagfile:
    tag_name = line.strip("<>\n")
    print tag_name

or use this:

for line in tagfile:
    line = line.rstrip()
    tag_name = line.strip("<>")
    print tag_name

Upvotes: 3

Related Questions