overexchange
overexchange

Reputation: 1

Trailing backslash characters in the source

For a line in a given source(f),

0x223f01 X 32 fhsjskk, \

below code still picks this line and print it.

for line in f:
            line.rstrip('\n') # every line has \n            
            if not line.endswith('\\'):
                print(line)

In this scenario, one cannot avoid trailing backslash from the source.

How to detect trailing backslash characters?

Upvotes: 0

Views: 42

Answers (2)

Ben Jones
Ben Jones

Reputation: 629

rstrip() returns the stripped string, it does not change the string itself. try:

line = line.rstrip('\n')

See: string.rstrip() Documentation

Upvotes: 3

TallChuck
TallChuck

Reputation: 1972

The rstrip function does not modify the original string, you need to change that line to line = line.rstrip('\n')

Upvotes: 1

Related Questions