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
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
yes.
just keep in mind that using eval() on untrusted data isn't a very good
idea.
</F>
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
> You could also use a csv file with a single row.
Err, I meant column, but a row would also work. Your choice.
Peter
You could also use a csv file with a single row.
Peter
>> 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>
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 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
>>> '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'
> 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