473,405 Members | 2,349 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,405 software developers and data experts.

Parsing tuple from string?

I have a list of strings, which I need to convert into tuples. If the
string is not in python tuple format (i.e. "('one', 'two')", "("one",
'two')", etc.), then I can just make it a 1-tuple (i.e. return
(string,) ). If it is in python tuple format, I need to parse it and
return the appropriate tuple (it's ok to keep all tuple elements as
strings).

I think eval() will work for this, but I don't know what will be in
the string, so I don't feel comfortable using that.

I tried coming up with a regex, but the best I can get with my limit
knowledge right now is:
matches = re.match("\(['\"](.*)['\"], ['\"](.*)['\"]\)", string)

which works well enough for my purposes (even though I know it's far
from proper), except that it only captures as 2-tuples, and I need to
be able to capture n-tuples. Can someone help point me to the correct
re or a better way to solve this?

Thanks in advance,
Daniel

Jun 27 '08 #1
3 3917
On Apr 23, 6:24*pm, Daniel <the.d...@gmail.comwrote:
I have a list of strings, which I need to convert into tuples. *If the
string is not in python tuple format (i.e. "('one', 'two')", "("one",
'two')", etc.), then I can just make it a 1-tuple (i.e. return
(string,) ). *If it is in python tuple format, I need to parse it and
return the appropriate tuple (it's ok to keep all tuple elements as
strings).

I think eval() will work for this, but I don't know what will be in
the string, so I don't feel comfortable using that.
Check out one of the safe restricted eval recipes, e.g.
http://preview.tinyurl.com/6h7ous.

HTH,
George
Jun 27 '08 #2
On Apr 23, 4:22 pm, George Sakkis <george.sak...@gmail.comwrote:
On Apr 23, 6:24 pm, Daniel <the.d...@gmail.comwrote:
I have a list of strings, which I need to convert into tuples. If the
string is not in python tuple format (i.e. "('one', 'two')", "("one",
'two')", etc.), then I can just make it a 1-tuple (i.e. return
(string,) ). If it is in python tuple format, I need to parse it and
return the appropriate tuple (it's ok to keep all tuple elements as
strings).
I think eval() will work for this, but I don't know what will be in
the string, so I don't feel comfortable using that.

Check out one of the safe restricted eval recipes, e.g.http://preview.tinyurl.com/6h7ous.
Thank you very much!
HTH,
George
Jun 27 '08 #3
On Apr 24, 12:21*am, Daniel <the.d...@gmail.comwrote:
On Apr 23, 4:22 pm, George Sakkis <george.sak...@gmail.comwrote:On Apr23, 6:24 pm, Daniel <the.d...@gmail.comwrote:
I have a list of strings, which I need to convert into tuples. *If the
string is not in python tuple format (i.e. "('one', 'two')", "("one",
'two')", etc.), then I can just make it a 1-tuple (i.e. return
(string,) ). *If it is in python tuple format, I need to parse it and
return the appropriate tuple (it's ok to keep all tuple elements as
strings).
I think eval() will work for this, but I don't know what will be in
the string, so I don't feel comfortable using that.
Check out one of the safe restricted eval recipes, e.g.http://preview.tinyurl.com/6h7ous.

Thank you very much!
You're welcome. By the way, there's a caveat: simple_eval() doesn't
require a comma between tuple/list elements or dict pairs:
>>simple_eval('(2 3)')
(2, 3)
>>simple_eval('[1 2 ,3]')
[1, 2, 3]
>>simple_eval('{ 1:"a" 2:"b"}')
{1: 'a', 2: 'b'}

Also parenthesized values are evaluated as 1-tuples
>>simple_eval('(2)')
(2,)

Since it doesn't evaluate general expressions anyway, that's not
necessarily a problem. In any case, making it strict is left as an
exercise to the reader :)

George
Jun 27 '08 #4

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

Similar topics

6
by: Artco News | last post by:
I thought I ask the scripting guru about the following. I have a file containing records of data with the following format(first column is the label): CODE#1^DESCRIPTION^CODE#2^NOTES...
1
by: .d.hos | last post by:
ok, fairly new to python, relatively familiar w/ ms-sql. here's my issue: my .py script parses the contents of a (tab delim.) flat file, then attempts to insert the information into the db. I've...
4
by: Alastair G. Hogge | last post by:
Hello *, I'm using Python and the cgi module to retrive data from a HTML form. I'm then trying to get that information into a string. But efforts fail when I run type(foo) on my string. type()...
2
by: Todd Moyer | last post by:
I would like to use Python to parse a *python-like* data description language. That is, it would have it's own keywords, but would have a syntax like Python. For instance: Ob1 ('A'): Ob2...
16
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I only know a little bit of xml and I'm trying to parse a xml document in order to save its elements in a file (dictionaries inside a list)....
15
by: Steve M | last post by:
Hello, I'm trying to figure out the index position of a tuple member. I know the member name, but I need to know the members index position. I know that if I use the statement print tuple that...
16
by: flyaflya | last post by:
a = "(1,2,3)" I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3)
3
by: localpricemaps | last post by:
i am having a problem writing a tuple to a text file. my code is below. what i end up getting is a text file that looks like this burger, 7up burger, 7up burger, 7up and this is instead...
10
by: len | last post by:
I have created the following program to read a text file which happens to be a cobol filed definition. The program then outputs to a file what is essentially a file which is a list definition...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.