Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Why is it that str.replace doesn't work sometimes?

Reply
Thread Tools

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

 
 
humn
Guest
Posts: n/a
 
      06-24-2009
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()
 
Reply With Quote
 
 
 
 
unayok
Guest
Posts: n/a
 
      06-24-2009
On Jun 24, 12:11*pm, humn <xelothat...@gmail.com> wrote:
> 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.

 
Reply With Quote
 
 
 
 
Ken Dyck
Guest
Posts: n/a
 
      06-24-2009
On Jun 24, 12:11*pm, humn <xelothat...@gmail.com> wrote:
> 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/lex...tring-literals

-Ken
 
Reply With Quote
 
humn
Guest
Posts: n/a
 
      06-24-2009
On Jun 24, 12:28*pm, unayok <una...@gmail.com> wrote:
> On Jun 24, 12:11*pm, humn <xelothat...@gmail.com> wrote:
>
> > 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.
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      06-24-2009
humn wrote:
> On Jun 24, 12:28 pm, unayok <una...@gmail.com> wrote:
>> On Jun 24, 12:11 pm, humn <xelothat...@gmail.com> wrote:
>>
>>> 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:

>>> # The empty string.
>>> ''

''
>>> ""

''
>>> # Quoting the other type.
>>> '"'

'"'
>>> "'"

"'"
>>> # Quoting the same type.
>>> '\''

"'"
>>> "\""

'"'
 
Reply With Quote
 
Lie Ryan
Guest
Posts: n/a
 
      06-24-2009
MRAB wrote:

> 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.
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      06-25-2009
On Jun 25, 7:17*am, Lie Ryan <lie.1...@gmail.com> wrote:
> MRAB wrote:
> > 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.


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"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
why why why does function not work Horace Nunley ASP .Net 1 09-27-2006 09:52 PM
Why oh why doesn't my data view work? David Prowak ASP .Net 1 01-30-2004 04:19 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57