By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,673 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,673 IT Pros & Developers. It's quick & easy.

repr(string)

P: n/a
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
Jul 23 '08 #1
Share this Question
Share on Google+
10 Replies

P: n/a
On Wed, Jul 23, 2008 at 12:04 PM, David C. Ullrich <du******@sprynet.comwrote:
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?
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
Jul 23 '08 #2

P: n/a
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.

</F>

Jul 23 '08 #3

P: n/a
In article <ma************************************@python.org >,
Fredrik Lundh <fr*****@pythonware.comwrote:
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...
</F>
--
David C. Ullrich
Jul 24 '08 #4

P: n/a
David C. Ullrich wrote:
In article <ma************************************@python.org >,
Fredrik Lundh <fr*****@pythonware.comwrote:
>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.

Peter
Jul 24 '08 #5

P: n/a
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

Jul 24 '08 #6

P: n/a
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>

Jul 24 '08 #7

P: n/a
In article <ma************************************@python.org >,
Fredrik Lundh <fr*****@pythonware.comwrote:
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... ;-)
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.)
</F>
--
David C. Ullrich
Jul 24 '08 #8

P: n/a
In article <g6*************@news.t-online.com>,
Peter Otten <__*******@web.dewrote:
David C. Ullrich wrote:
In article <ma************************************@python.org >,
Fredrik Lundh <fr*****@pythonware.comwrote:
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
Jul 24 '08 #9

P: n/a
On Jul 23, 4:04*pm, "David C. Ullrich" <dullr...@sprynet.comwrote:
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'
Jul 25 '08 #10

P: n/a
In article
<c5**********************************@a6g2000prm.g ooglegroups.com>,
MRAB <go****@mrabarnett.plus.comwrote:
On Jul 23, 4:04*pm, "David C. Ullrich" <dullr...@sprynet.comwrote:
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
Jul 25 '08 #11

This discussion thread is closed

Replies have been disabled for this discussion.