RTF Parsing

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Jul 30 06:38:12 CEST 2008

En Tue, 29 Jul 2008 10:08:21 -0300, Victor Subervi  
<victorsubervi at gmail.com> escribi�:

> Hi;
> I have this code:
> def a():
> chars = ['\\i0', '\\u0', '\\qc', '\\b0', '\\ql', '\\i', '\\u', '\\b',
> '\\yz']
> rtf_markup = 'viewkind4\uc1\pard\nowidctlpar\qc\i\f0\fs36 Who is like the
> Beast? Who can wage war against him?\par'

The \ is an escape character inside string literals. '\n' contains a  
*single* character and it's the same as chr(10). '\f0' is two characters  
long, not three. You have two alternatives:

a) double each \ (because \\ is interpreted as a single backslash):
rtf_markup = 'viewkind4\\uc1\\pard\\nowidctlpar\\qc...'

b) use raw strings - that is, prefix each string literal with a small r:
rtf_markup = r'viewkind4\uc1\pard\nowidctlpar...'

See the tutorial  
http://docs.python.org/tut/node5.html#SECTION005120000000000000000 and the  
gory details at http://docs.python.org/ref/strings.html

Gabriel Genellina

More information about the Python-list mailing list