regsub.gsub eats backslashes
Richard van de Stadt
stadt at cs.utwente.nl
Wed Mar 29 17:55:10 CEST 2000
I had a little fight with Python over a bunch of backslashes.
I am generating proceedings. I.e. I am generating LaTeX.
I have a proceedings template, with some keywords, which I replace by
the contents of files, using regsub.gsub, e.g.:
p = template
p = regsub.gsub ('_PROGRAMCOMMITTEE_', programcommittee, p)
programcommittee is read from a file that contains TeX. Each of
the lines in that file, containing a program committee member's
name has to end in 2 backslashes (which means a newline in TeX).
For the above substitution to work, I have to put 4 backslashes
in the file that contains the program committee stuff.
Ok, this seems reasonable. To get one backslash in Python, you
should escape it with a backslash. I need 2, so I have to put
in 4. So far, so good.
Another keyword, _REFEREES_ should be replaced by the list of
co-reviewers. The difference with the program committee file is
that the referees file only contains the names, no TeX stuff.
These names are to be put in the proceedings in 2 columns.
Therefore, this file with referee names is read and the TeX
stuff for the columns is added to the lines with the names,
using regsub.gsub(). (The end of each line is replace with
some backslashes and more stuff.)
Then, the _REFEREES_ keyword is being replaced by this generated
list, again using regsub.gsub(). To my surprise, to end up with
2 backslashes in the resulting file, I need to put 10 (!)
backslashes in the regsub that does the column stuff.
I can't figure out why regsub eats so many backslashes. Seeing the
result of the PROGRAMCOMMITTEE substitution, 8 backslashes might
have been more reasonable, because there is an extra regsub.gsub().
Does anyone have a clue? Is there a better way to do this?
I'm using Python 1.5.2 on Solaris.
More information about the Python-list