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 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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...
|
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
|
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')
|
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:
| |
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
|
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)
|
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
|
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
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |