473,770 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3931
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
1903
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 NN-110^an info of NN-001^BRY234^some notes NN-111^1st line data 2nd line data 3rd line data^BRT345^another notes
1
2668
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 been fighting this for a day or so, and i'm stuck on the db insertion... basically the script uses .readlines() to capture the flat file contents, and stick the *record* into a container list...
4
9186
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() returns tuple. My codes follows: #!/usr/local/bin/python import cgi
2
2180
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 ('B'): Ob3 ('D') Ob3 ('E') Ob2 ('C')
16
5996
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). When I access a url from python 2.3.3 running in Linux with the following lines:
15
4045
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 it will print the contents of that location. What I don't understand is if I know that foo is a member of tuple, how do I get foo's index position. Thanks-in-Advance Steve
16
23016
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
2756
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 of getting a list that should look like this
10
1997
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 which I can later copy and past into a python program. I will eventually expand the program to also output an SQL script to create a SQL file in MySQL The program still need a little work, it does not handle the following items
0
9454
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
10101
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...
0
9906
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
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...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
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
5354
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...
1
4007
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
3609
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.