472,362 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,362 software developers and data experts.

repr(string)

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
10 1495
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John F Dutcher | last post by:
I use code like the following to retrieve fields from a form: recd = recd.append(string.ljust(form.getfirst("lname",' '),15)) recd.append(string.ljust(form.getfirst("fname",' '),15)) etc.,...
1
by: Edward C. Jones | last post by:
On 2003-09-04, Rasmus Fogh said: > I need a way of writing strings or arbitrary Python code that will > > a) allow the strings to be read again unchanged (like repr) > b) write multiline...
7
by: Kamilche | last post by:
I want to convert a dict into string form, then back again. After discovering that eval is insecure, I wrote some code to roll a Python object, dict, tuple, or list into a string. I've posted it...
2
by: Mark Tolonen | last post by:
I don't understand the behavior of the interpreter in Python 3.0. I am working at a command prompt in Windows (US English), which has a terminal encoding of cp437. In Python 2.5: Python 2.5...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.