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 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
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>
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
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
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
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>
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
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
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'
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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.,...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |