Why is it that str.replace doesn't work sometimes?

H

humn

I'm writing a script to convert Latex commands to bbcode by using the
str.replace function and I'm confused as to why this works:

if '\chapter' in line:
line = line.replace('\chapter{', '')
line = line.replace('}', '
')

but this doesn't:

if '\title' in line:
line = line.replace('\title{', '')
line = line.replace('}', '
')

Here is the short version of the script:

infile = open('test.tex')

outfilename = infile.name.partition('.')[0] + '.bbcode'
outfile = open(outfilename, 'w')

for line in infile:

if '\title' in line:
line = line.replace('\title{', '')
line = line.replace('}', '
\n')

if '\chapter' in line:
line = line.replace('\chapter{', '')
line = line.replace('}', '
')

outfile.write(line)

infile.close()
outfile.close()
 
U

unayok

but this doesn't:

if '\title' in line:
        line = line.replace('\title{', '')
        line = line.replace('}', '
')

\t is an escaped <tab> character. so, '\title' will look for
'<tab>itle'

Two ways to fix this:

1. use r'\title'

2. use '\\title'

\c does not represent a known escape sequence so it remains two
characters.
 
K

Ken Dyck

I'm confused as to why this works:

if '\chapter' in line:
        line = line.replace('\chapter{', '')
        line = line.replace('}', '
')

but this doesn't:

if '\title' in line:
        line = line.replace('\title{', '')
        line = line.replace('}', '
')

In string literals---whether they use single or double quotes---
backslashes are used as escape characters to denote special
characters. The '\t' in '\title' is interpreted as the tab character
so the string that your code is trying to find and replace is actually
'<TAB>itle'. There isn't any special meaning for '\c', so python
interprets that to mean a backslash followed by the character 'c',
which is why the first case works.

There are two ways to solve the problem:

1. Prefix the string literal with an 'r', indicating that backslashes
should not be treated as escape characters (eg. r'\title'), or
2. Use a double backslash in the string literal to indicate that you
mean a literal backslash, not an escape character (eg. '\\title')

The official documentation, including a list of the special escape
sequences, is here:
http://docs.python.org/reference/lexical_analysis.html#string-literals

-Ken
 
H

humn

but this doesn't:
if '\title' in line:
        line = line.replace('\title{', '')
        line = line.replace('}', '
')

\t is an escaped <tab> character. so, '\title' will look for
'<tab>itle'

Two ways to fix this:

1. use r'\title'

2. use '\\title'

\c does not represent a known escape sequence so it remains two
characters.

Thank you! Didn't know that it would escape characters with single
quotes. I thought it only did that with double quotes.
 
M

MRAB

humn said:
but this doesn't:
if '\title' in line:
line = line.replace('\title{', '')
line = line.replace('}', '
')
\t is an escaped <tab> character. so, '\title' will look for
'<tab>itle'

Two ways to fix this:

1. use r'\title'

2. use '\\title'

\c does not represent a known escape sequence so it remains two
characters.

Thank you! Didn't know that it would escape characters with single
quotes. I thought it only did that with double quotes.

There's no difference between the two types of quote character. Python
itself prefers to show ' when printing, unless " would be clearer:
'"'
 
L

Lie Ryan

MRAB said:
There's no difference between the two types of quote character.

a small exception is single-quote can contain double quote but cannot
contain unescaped single quote while double-quote can contain single
quote but cannot contain unescaped double quote.
 
J

John Machin

a small exception is single-quote can contain double quote but cannot
contain unescaped single quote while double-quote can contain single
quote but cannot contain unescaped double quote.

Refinement 1:
S can contain D but cannot contain unescaped S
D can contain S but cannot contain unescaped D

Refinement 2:
for Q in list_of_defined_quote_characters:
Q can contain non-Q but cannot contain unescaped Q

I'd call that orthogonal and a similarity not a small exception to "no
difference" :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,052
Members
47,656
Latest member
rickwatson

Latest Threads

Top