David C. Ullrich dullrich at
Fri Jul 25 17:42:58 CEST 2008

In article 
<c55ff9a0-abb9-48a3-990c-c643d61b8641 at>,
 MRAB <google at> wrote:

> On Jul 23, 4:04 pm, "David C. Ullrich" <dullr... at> 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?

Ah, you certainly might! Seems exactly right, thanks.

Many years ago I thought I had some idea of what was available
in Python - these days it's full of all this neat stuff I never
heard of...

> >>> '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'

David C. Ullrich

More information about the Python-list mailing list