python - Why can't I remove quotes using `strip('\"')`? -


i can remove left quotes using str.strip('\"'):

with open(filename, 'r') fp :     line in fp.readlines() :         print(line)         line = line.strip('\"')         print(line) 

part of results:

"route d'espagne"  route d'espagne" 

using line.replace('\"', '') gets right result:

"route d'espagne"  route d'espagne 

can explain it?

your lines not end quotes. newline separator part of line , not removed when reading file, unless include \n in set of characters stripped " going stay.

when diagnosing issues strings, produce debug output print(repr(line)) or print(ascii(line)), make non-printable or non-ascii codepoints visible:

>>> line = '"route d\'espagne"\n' >>> print(line) "route d'espagne"  >>> print(repr(line)) '"route d\'espagne"\n' 

add \n str.strip() argument:

line = line.strip('"\n') 

demo:

>>> line.strip('"') 'route d\'espagne"\n' >>> line.strip('"\n') "route d'espagne" >>> print(line.strip('"\n')) route d'espagne 

Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -