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

humn xelothath13 at gmail.com
Wed Jun 24 12:11:22 EDT 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{', '[b][u]')
        line = line.replace('}', '[/b][/u]')

but this doesn't:

if '\title' in line:
        line = line.replace('\title{', '[size=150][b]')
        line = line.replace('}', '[/b][/size]')

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{', '[size=150][b]')
        line = line.replace('}', '[/b][/size]\n')

    if '\chapter' in line:
        line = line.replace('\chapter{', '[b][u]')
        line = line.replace('}', '[/u][/b]')

    outfile.write(line)

infile.close()
outfile.close()



More information about the Python-list mailing list