473,320 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1541
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.