repr(string)

MRAB google at mrabarnett.plus.com
Thu Jul 24 20:43:19 EDT 2008


On Jul 23, 4:04 pm, "David C. Ullrich" <dullr... at sprynet.com> wrote:
> I've been saving data in a file with one line per field.
> Now some of the fields may become multi-line strings...
>
> I was about to start escaping and unescaping linefeeds
> by hand, when I realized that repr() and eval() should
> do. Hence the question: If s is a string, is repr(s)
> guaranteed not to contain line breaks?
>
Might I suggest you use encode and decode instead?

>>> 'first line\nsecond line'.encode('string-escape')
'first line\\nsecond line'
>>> _.decode('string-escape')
'first line\nsecond line'
>>> u'first line\nsecond line'.encode('unicode-escape')
'first line\\nsecond line'
>>> _.decode('unicode-escape')
u'first line\nsecond line'



More information about the Python-list mailing list