Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

repr(string)

0 views
Skip to first unread message

David C. Ullrich

unread,
Jul 23, 2008, 11:04:52 AM7/23/08
to
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?

--
David C. Ullrich

Guilherme Polo

unread,
Jul 23, 2008, 11:12:37 AM7/23/08
to David C. Ullrich, pytho...@python.org

In the sense that line breaks are quoted ? Yes, repr does that.

>
> --
> David C. Ullrich
> --
> http://mail.python.org/mailman/listinfo/python-list
>

--
-- Guilherme H. Polo Goncalves

Fredrik Lundh

unread,
Jul 23, 2008, 4:06:18 PM7/23/08
to pytho...@python.org
David C. Ullrich wrote:

yes.

just keep in mind that using eval() on untrusted data isn't a very good
idea.

</F>

David C. Ullrich

unread,
Jul 24, 2008, 11:12:39 AM7/24/08
to
In article <mailman.561.1216843...@python.org>,
Fredrik Lundh <fre...@pythonware.com> wrote:

Right. This data comes from me, gets put into a file and then
read by me. Someone _could_ corrupt that file, but someone who
could do that could more easily just throw the machine out
the window...

> </F>

--
David C. Ullrich

Peter Otten

unread,
Jul 24, 2008, 11:27:38 AM7/24/08
to
Peter Otten wrote:

> You could also use a csv file with a single row.

Err, I meant column, but a row would also work. Your choice.

Peter

Peter Otten

unread,
Jul 24, 2008, 11:25:56 AM7/24/08
to
David C. Ullrich wrote:

You could also use a csv file with a single row.

Peter

Fredrik Lundh

unread,
Jul 24, 2008, 11:35:40 AM7/24/08
to pytho...@python.org
David C. Ullrich skrev:

>> just keep in mind that using eval() on untrusted data isn't a very good
>> idea.
>
> Right. This data comes from me, gets put into a file and then
> read by me. Someone _could_ corrupt that file, but someone who
> could do that could more easily just throw the machine out
> the window...

and then your boss finds your program useful, and it's installed on a
shared server, and then the guys at the office in Eggkleiva wants a
copy, and then people start shipping save files via mail to keep things
synchronized, and then someone sets up a web service... ;-)

</F>

David C. Ullrich

unread,
Jul 24, 2008, 12:21:58 PM7/24/08
to
In article <mailman.602.1216913...@python.org>,
Fredrik Lundh <fre...@pythonware.com> wrote:

Heh-heh. Good point, except that the idea that someone's going to
find it useful is utterly implausible. Nobody but me has ever found
a program I wrote useful. People think it's funny that I write little
Python programs to do things I could just do in Excel or Open
Office. (When I have some accounting/secretarial sort of thing
to do doing it by hand in Python is one way to make it tolerably
interesting. Easier to add new features - instead of trying to find
an Excel way to do something like delete the smallest _two_
items in a list I just do it.)

David C. Ullrich

unread,
Jul 24, 2008, 12:26:12 PM7/24/08
to
In article <g6a6u2$kfk$03$1...@news.t-online.com>,
Peter Otten <__pet...@web.de> wrote:

> David C. Ullrich wrote:
>
> > In article <mailman.561.1216843...@python.org>,
> > Fredrik Lundh <fre...@pythonware.com> wrote:
> >
> >> David C. Ullrich 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?
> >>
> >> yes.
> >>
> >> just keep in mind that using eval() on untrusted data isn't a very good
> >> idea.
> >
> > Right. This data comes from me, gets put into a file and then
> > read by me. Someone _could_ corrupt that file, but someone who
> > could do that could more easily just throw the machine out
> > the window...
>
> You could also use a csv file with a single row.

Excellent suggestion. I have a different point of view on all
this than most of you guys (see reply to F). From my curious
point of view csv was no fun anymore when csvlib got added to Python...

> Peter

--
David C. Ullrich

MRAB

unread,
Jul 24, 2008, 8:43:19 PM7/24/08
to
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'

David C. Ullrich

unread,
Jul 25, 2008, 11:42:58 AM7/25/08
to
In article
<c55ff9a0-abb9-48a3...@a6g2000prm.googlegroups.com>,
MRAB <goo...@mrabarnett.plus.com> wrote:

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

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

0 new messages