473,657 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1558
On Wed, Jul 23, 2008 at 12:04 PM, David C. Ullrich <du******@spryn et.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************ *************** *********@pytho n.org>,
Fredrik Lundh <fr*****@python ware.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************ *************** *********@pytho n.org>,
Fredrik Lundh <fr*****@python ware.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************ *************** *********@pytho n.org>,
Fredrik Lundh <fr*****@python ware.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************ *************** *********@pytho n.org>,
Fredrik Lundh <fr*****@python ware.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...@spryn et.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('s tring-escape')
'first line\\nsecond line'
>>_.decode('str ing-escape')
'first line\nsecond line'
>>u'first line\nsecond line'.encode('u nicode-escape')
'first line\\nsecond line'
>>_.decode('uni code-escape')
u'first line\nsecond line'
Jul 25 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
7996
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., etc. The intent is to finish by assigning the list to a string that I would write to disk: recstr = string.join(recd,'')
1
2386
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 strings as multiline strings instead of escaping > the \n's. > > A repr function that output triple-quoted strings with explicit > (non-escaped) linebreaks would be perfect.
7
1688
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 below. Does anyone know an easier way to accomplish this? Essentially, I want to avoid doing an 'eval' on a string to get it back into dict form... but still allow nested structures. (My current code doesn't handle nested structures.) I conked...
2
2720
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 (r25:51908, Sep 19 2006, 09:52:17) on win 32 Type "help", "copyright", "credits" or "license" for more information. u'\u5000'
0
8323
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.