473,231 Members | 1,592 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,231 software developers and data experts.

Is there a simple way to parse this string ?

hello,

I need to translate the following string
a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'

into the following list or tuple
b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]

Is there a simple way to to this.
(Not needed now, but might need it in the future: even deeper nested
lists, represented by a string.)

thanks,
Stef Mientki
Dec 19 '07 #1
11 1317
Stef,

You can quickly get a tuple via:

t = eval('(0, 0, 0, 255), (192, 192, 192, 255), True, 8')

Joseph Armbruster

On Dec 19, 4:17 pm, Stef Mientki <stef.mien...@gmail.comwrote:
hello,

I need to translate the following string
a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'

into the following list or tuple
b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]

Is there a simple way to to this.
(Not needed now, but might need it in the future: even deeper nested
lists, represented by a string.)

thanks,
Stef Mientki
Dec 19 '07 #2
Stef Mientki wrote:
hello,

I need to translate the following string
a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'

into the following list or tuple
b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]

Is there a simple way to to this.
(Not needed now, but might need it in the future: even deeper nested
lists, represented by a string.)

thanks,
Stef Mientki
There are threads on this list about eval and how you need to be careful.
Make sure you know where the string is coming from and can control the
contents. If you read if from a user they could type in:

os.system('rm -rf *') or os.system('del *.*')

eval that and it deletes all the files on your disk

-Larry
Dec 19 '07 #3
On Dec 20, 9:10 am, Larry Bates <larry.ba...@websafe.comwrote:
Stef Mientki wrote:
hello,
I need to translate the following string
a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
into the following list or tuple
b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]
Is there a simple way to to this.
(Not needed now, but might need it in the future: even deeper nested
lists, represented by a string.)
thanks,
Stef Mientki

There are threads on this list about eval and how you need to be careful.
In particular Paul Maguire recently pointed to a safe evaluator that
was restricted (IIRC) to something like lists/dicts/etc of ints/floats/
string/etc constants -- looks like just what you need.
Make sure you know where the string is coming from and can control the
contents. If you read if from a user they could type in:

os.system('rm -rf *') or os.system('del *.*')

eval that and it deletes all the files on your disk
Does anyone know of a newsreader that can automatically killfile
people who suggest eval without any warnings at all? Or should we let
Darwinian selection take its effect?
Dec 19 '07 #4
En Wed, 19 Dec 2007 19:23:36 -0300, John Machin <sj******@lexicon.net>
escribió:
On Dec 20, 9:10 am, Larry Bates <larry.ba...@websafe.comwrote:
>Stef Mientki wrote:
I need to translate the following string
a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
into the following list or tuple
b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]
Is there a simple way to to this.

In particular Paul Maguire recently pointed to a safe evaluator that
was restricted (IIRC) to something like lists/dicts/etc of ints/floats/
string/etc constants -- looks like just what you need.
There is also a Cookbook recipe for a safe_eval function at
http://aspn.activestate.com/ASPN/Coo.../Recipe/364469
Does anyone know of a newsreader that can automatically killfile
people who suggest eval without any warnings at all? Or should we let
Darwinian selection take its effect?
Doesn't work, Darwininan selection would act on the unfortunate people
asking, not on who careless answers "use eval" :(

--
Gabriel Genellina

Dec 20 '07 #5
On Dec 19, 4:23 pm, John Machin <sjmac...@lexicon.netwrote:
On Dec 20, 9:10 am, Larry Bates <larry.ba...@websafe.comwrote:


Stef Mientki wrote:
hello,
I need to translate the following string
a = '(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
into the following list or tuple
b = [(0, 0, 0, 255), (192, 192, 192, 255), True, 8 ]
Is there a simple way to to this.
(Not needed now, but might need it in the future: even deeper nested
lists, represented by a string.)
thanks,
Stef Mientki
There are threads on this list about eval and how you need to be careful.

In particular Paul Maguire recently pointed to a safe evaluator that
was restricted (IIRC) to something like lists/dicts/etc of ints/floats/
string/etc constants -- looks like just what you need.
I think the last thread of this nature also cited a similar tool by
the effbot, which he describes here: http://www.effbot.org/zone/simple-iterator-parser.htm.
This parser is about 10X faster than the equivalent pyparsing parser.

-- Paul (McGuire)
Dec 20 '07 #6
On Dec 19, 4:23 pm, John Machin <sjmac...@lexicon.netwrote:
On Dec 20, 9:10 am, Larry Bates <larry.ba...@websafe.comwrote:

In particular Paul Maguire recently pointed to a safe evaluator that
was restricted (IIRC) to something like lists/dicts/etc of ints/floats/
string/etc constants -- looks like just what you need.
The pyparsing parser can be viewed at
http://pyparsing.wikispaces.com/spac...PythonValue.py.

-- Paul
Dec 20 '07 #7
On Dec 19, 8:44 pm, Paul McGuire <pt...@austin.rr.comwrote:
I think the last thread of this nature also cited a similar tool by
the effbot, which he describes here:http://www.effbot.org/zone/simple-iterator-parser.htm.
This parser is about 10X faster than the equivalent pyparsing parser.
Here's the relevant thread: http://preview.tinyurl.com/2aeswn. Note
that the builtin eval() is around 5x faster than this parser, and from
the statement above, 50x faster than the pyparsing solution.

George
Dec 20 '07 #8
Stef,

For clarification, there is nothing hazardous about using eval on the
string that you presented.

t = eval('(0, 0, 0, 255), (192, 192, 192, 255), True, 8')

Whether or not this is the "simplest" solution, remains a question.
Dec 21 '07 #9
On Thu, 20 Dec 2007 20:27:23 -0800, jo**************@gmail.com wrote:
Stef,

For clarification, there is nothing hazardous about using eval on the
string that you presented.

t = eval('(0, 0, 0, 255), (192, 192, 192, 255), True, 8')

Whether or not this is the "simplest" solution, remains a question.

For clarification, if all the poster wanted was to convert the *specific*
*known* string to a tuple, he would be better off just writing it as a
tuple:

t = (0, 0, 0, 255), (192, 192, 192, 255), True, 8

is much faster than calling eval().

But obviously that's not what the Original Poster wants to do. The tuple
give was indicative of input that comes from somewhere -- perhaps a
config file, perhaps a web form, perhaps a command line argument, who
knows? The point is, if the string comes from a user, then it could
contain anything:

'(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
'1000, 10001, 100002, 1000004'
'"foo bar baz".split()'
'[i for i in range(100000)]'
'[19852.7412]*100000**2'
'__import__("os").system("ls -r *")'
Just because the OP's specific example is safe doesn't make eval() safe.
--
Steven
Dec 21 '07 #10
Steven D'Aprano,

On Dec 21, 2:08 am, Steven D'Aprano
<ste...@REMOVE.THIS.cybersource.com.auwrote:
On Thu, 20 Dec 2007 20:27:23 -0800, josepharmbrus...@gmail.com wrote:
Stef,
For clarification, there is nothing hazardous about using eval on the
string that you presented.
t = eval('(0, 0, 0, 255), (192, 192, 192, 255), True, 8')
Whether or not this is the "simplest" solution, remains a question.

For clarification, if all the poster wanted was to convert the *specific*
*known* string to a tuple, he would be better off just writing it as a
tuple:
Steven,

No, that's not what he asked. Read the original question.
>
t = (0, 0, 0, 255), (192, 192, 192, 255), True, 8

is much faster than calling eval().

But obviously that's not what the Original Poster wants to do.
There's nothing "Obviously" Implied about what the author wants to do
here, besides convert an innocent string object to a tuple.
The tuple
give was indicative of input that comes from somewhere
Really? I personally can't tell that from his provided example.
There's definitely not enough info on this one.
-- perhaps a
config file, perhaps a web form, perhaps a command line argument, who
knows? The point is, if the string comes from a user, then it could
contain anything:

'(0, 0, 0, 255), (192, 192, 192, 255), True, 8'
'1000, 10001, 100002, 1000004'
'"foo bar baz".split()'
'[i for i in range(100000)]'
'[19852.7412]*100000**2'
'__import__("os").system("ls -r *")'

Just because the OP's specific example is safe doesn't make eval() safe.
Agreed. And after the last couple comments, he was probably made
aware of that. Thank you for reiterating :-)
>
--
Steven
Dec 22 '07 #11
On Sat, 22 Dec 2007 07:21:26 -0800, jo**************@gmail.com wrote:
Steven D'Aprano,

On Dec 21, 2:08 am, Steven D'Aprano
<ste...@REMOVE.THIS.cybersource.com.auwrote:
>On Thu, 20 Dec 2007 20:27:23 -0800, josepharmbrus...@gmail.com wrote:
Stef,
For clarification, there is nothing hazardous about using eval on the
string that you presented.
t = eval('(0, 0, 0, 255), (192, 192, 192, 255), True, 8')
Whether or not this is the "simplest" solution, remains a question.

For clarification, if all the poster wanted was to convert the
*specific* *known* string to a tuple, he would be better off just
writing it as a tuple:

Steven,

No, that's not what he asked. Read the original question.
I did. I even read all the way down to the part where he wrote:

"(Not needed now, but might need it in the future: even deeper nested
lists, represented by a string.)"

Its clear that the OP has more in mind than just a single specific known
string.

--
Steven
Dec 23 '07 #12

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

Similar topics

10
by: george young | last post by:
For each run of my app, I have a known set of (<100) wafer names. Names are sometimes simply integers, sometimes a short string, and sometimes a short string followed by an integer, e.g.: 5, 6,...
19
by: gaudetteje | last post by:
I've been searching high and low for a way to simply convert a small XML configuration file to Python data structures. I came across gnosis XML tools, but need a built-in option for doing...
15
by: Jeannie | last post by:
Hello group! I'm in Europe, traveling with my laptop, and I don't any compilers other than Borland C++ 5.5. available. I also don't have any manuals or help files available. Sadly, more...
5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
3
by: billr | last post by:
Excuse me if you consider this a cross post, however, it was originally destined for this group not the vc.atl group, but somehow it went there instead of here! (obviously it was something I did, but...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
10
by: leaf | last post by:
How do i make a simple parser that parse strings ex. "string1 string2 string3" and store in a vector? how can it be done using BOOST.Spirit? --- leaf
0
by: Grzegorz Smith | last post by:
Hi All. I 'm learning ZSI to use SOAP and I desperately need help. I'm working on example from tutorial -(examples/server/send_response/ simple/wsdl/). Here are my wsdl files...
2
by: Dragan | last post by:
Hi, We're working in VS 2005, Team edition, if it makes any difference at all (should be up-to-date and all that, but could not guarantee it is 100%). We've implemented a simple generic wrapper...
5
by: newsaboutgod | last post by:
I think VB.NET drives some people crazy because some simple VB6 things seem so hard. Here is some VB6 code: 'Write CSV File open "c:\test.csv" for output as #1 write#1, "1","2","3","4","5"...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.