473,756 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Extract String From Enclosing Tuple

I'm a bit embarrassed to have to ask for help on this, but I'm not finding
the solution in the docs I have here.

Data are assembled for writing to a database table. A representative tuple
looks like this:

('eco', "(u'Roads', )", 0.0739698873013 48305)

Pysqlite doesn't like the format of the middle term:
pysqlite2.dbapi 2.InterfaceErro r: Error binding parameter 1 - probably
unsupported type.

I want to extract the 'Roads', part from the double-quoted enclosing
tuple. The unicode part should be automatically removed when the string is
printed, but I suspect it's the double quotes and extra parentheses that are
the problem. I know that tuples are immutable, but I thought that I could
slice it. If so, I'm not doing it correctly, because each attempt results in
TypeError: unsubscriptable object

Even when I assign that middle term to a variable before assembling the
tuple for insertion in the database, I just cannot get the job done. Whether
in the tuple of three terms or by itself, I haven't applied the proper
technique.

Insight appreciated.

Rich
Feb 28 '07 #1
12 4165
rs******@nospam .appl-ecosys.com a écrit :
I'm a bit embarrassed to have to ask for help on this, but I'm not finding
the solution in the docs I have here.

Data are assembled for writing to a database table. A representative tuple
looks like this:

('eco', "(u'Roads', )", 0.0739698873013 48305)

Pysqlite doesn't like the format of the middle term:
pysqlite2.dbapi 2.InterfaceErro r: Error binding parameter 1 - probably
unsupported type.

I want to extract the 'Roads', part from the double-quoted enclosing
tuple. The unicode part should be automatically removed when the string is
printed, but I suspect it's the double quotes and extra parentheses that are
the problem. I know that tuples are immutable, but I thought that I could
slice it. If so, I'm not doing it correctly, because each attempt results in
TypeError: unsubscriptable object
Where do you get your data from ? MHO is that you'd better handle the
problem at the source (ie : dont put a string representation of a tuple
in your data) instead of trying to fix it afterward.
Feb 28 '07 #2
On Feb 28, 12:40 pm, rshep...@nospam .appl-ecosys.com wrote:
I'm a bit embarrassed to have to ask for help on this, but I'm not finding
the solution in the docs I have here.

Data are assembled for writing to a database table. A representative tuple
looks like this:

('eco', "(u'Roads', )", 0.0739698873013 48305)

Pysqlite doesn't like the format of the middle term:
pysqlite2.dbapi 2.InterfaceErro r: Error binding parameter 1 - probably
unsupported type.

I want to extract the 'Roads', part from the double-quoted enclosing
tuple.
(snipped)

Perhaps something like:
>>t = ('eco', "(u'Roads', )", 0.0739698873013 48305)
t2 = eval(t[1], { '__builtins__' : {} }, {} )
t2
(u'Roads',)
>>t2[0].encode('ascii' )
'Roads'

>>import itertools
tuple(itertoo ls.chain((t[0], t2[0].encode('ascii' )), t[2:]))
('eco', 'Roads', 0.0739698873013 48305)
>>tuple(itertoo ls.chain((t[0], (t2[0].encode('ascii' ),)), t[2:]))
('eco', ('Roads',), 0.0739698873013 48305)
--
Hope this helps,
Steven

Feb 28 '07 #3
rs******@nospam .appl-ecosys.com writes:
Data are assembled for writing to a database table. A
representative tuple looks like this:

('eco', "(u'Roads', )", 0.0739698873013 48305)
You refer to the second item as "a tuple" later, but it's not; it's
now just a string (not even a unicode string). Whatever has assembled
these has effectively lost the structure of the data and you are now
left with three items in a tuple: string, string, float.

Some RDBMSs can store a structure of multiple values in one value;
SQLite cannot. The usual solution for this limitation is to take these
structural values and store the component values as separate rows of a
different table, and have each of those rows refer back to the
identifying key of the original table so they can be joined easily.

E.g., I might conceptually think of order records as the following
tuples, with further tuples-of-tuples for the items on each order:

orders = (
# fields: id, cust_code, date, order_items
(1, "cust1234", "2007-02-15", (("item002", 1), ("item005", 3), ("item007", 1))),
(2, "cust4567", "2007-02-19", (("item001", 5), ("item005", 2))),
)

Since I can't store those as-is in SQLite, I would need to restructure
the tables: separate the "order items" to separate rows in a dedicated
table, and refer to the "order" table key in each "order item" row.

orders = (
# fields: id, cust_code, date
(1, "cust1234", "2007-02-15"),
(2, "cust4567", "2007-02-19"),
)

order_items = (
# fields: id, order_id, item_code, quantity
(1, 1, "item002", 1),
(2, 1, "item005", 3),
(3, 1, "item007", 1),
(4, 2, "item001", 5),
(5, 2, "item005", 2),
)

Then you can use SQL JOIN clauses as necessary, with the
order_item.orde r_id field a foreign key into the order table.

--
\ "I moved into an all-electric house. I forgot and left the |
`\ porch light on all day. When I got home the front door wouldn't |
_o__) open." -- Steven Wright |
Ben Finney

Feb 28 '07 #4
Ben Finney <bi************ ****@benfinney. id.auwrites:
orders = (
[...]
)

order_items = (
[...]
)
For clarity, these sequences of records should be lists (with each
item being a tuple containing the record fields), not tuples.

A tuple implies a meaning associated with each position in the
sequence (like a record with a positional meaning for each field), a
list implies the opposite (a sequence with order but not meaning
associated with each position).

--
\ "If you were going to shoot a mime, would you use a silencer?" |
`\ -- Steven Wright |
_o__) |
Ben Finney

Feb 28 '07 #5
Ben Finney wrote:
A tuple implies a meaning associated with each position in the
sequence (like a record with a positional meaning for each field),
a list implies the opposite (a sequence with order but not meaning
associated with each position).
Explain. I know tuples as immutable lists ...

BTW, don't confuse python tuples with tuples from mathematics.

Regards,
Björn

--
BOFH excuse #214:

Fluorescent lights are generating negative ions. If turning them off
doesn't work, take them out and put tin foil on the ends.

Mar 1 '07 #6
Bjoern Schliessmann <us************ **************@ spamgourmet.com writes:
Ben Finney wrote:
A tuple implies a meaning associated with each position in the
sequence (like a record with a positional meaning for each field),
a list implies the opposite (a sequence with order but not meaning
associated with each position).

Explain.
Well, since you ask so politely :-)
I know tuples as immutable lists ...
That's a common misconception.

Tuples are intended for use as heterogeneous data structures: every
index in the sequence *means* something, a semantic meaning applied to
the item at that index. It's for this reason that a tuple is
immutable: removing items, inserting them in the middle, etc. would
imply that the index doesn't have semantic meaning for the structure,
which is not true.

Lists are intended for use as homogeneous sequences: not that every
value is of the same type, but that a particular index in the sequence
doesn't *mean* anything about the semantic interpretation of the item
at that position. It's for this reason that a list is mutable: since
the index of an item has no semantic meaning, inserting new items or
removing them from anywhere in the sequence doesn't alter the meaning
of the structure.

James Tauber explains further:

<URL:http://jtauber.com/blog/2006/04/15/python_tuples_a re_not_just_con stant_lists>

--
\ "You can be a victor without having victims." -- Harriet Woods |
`\ |
_o__) |
Ben Finney

Mar 1 '07 #7
On Feb 28, 10:45 pm, Ben Finney <bignose+hate s-s...@benfinney. id.au>
wrote:
Bjoern Schliessmann <usenet-mail-0306.20.chr0n.. .@spamgourmet.c omwrites:
I know tuples as immutable lists ...

That's a common misconception.
And this catch phrase, "that's a common misconception", is a common
aping of the BDFL's take on this. As several long threads have shown,
it is a highly controversial topic and claiming that one side has
misconceived it doesn't make it more true than a Catholic claiming
that Protestants are misconceived about the True Christianity or vice
versa.
Tuples are intended for use as heterogeneous data structures: every
index in the sequence *means* something, a semantic meaning applied to
the item at that index. It's for this reason that a tuple is
immutable: removing items, inserting them in the middle, etc. would
imply that the index doesn't have semantic meaning for the structure,
which is not true.

Lists are intended for use as homogeneous sequences: not that every
value is of the same type, but that a particular index in the sequence
doesn't *mean* anything about the semantic interpretation of the item
at that position. It's for this reason that a list is mutable: since
the index of an item has no semantic meaning, inserting new items or
removing them from anywhere in the sequence doesn't alter the meaning
of the structure.

James Tauber explains further:

<URL:http://jtauber.com/blog/2006/04/15/python_tuples_a re_not_just_con stan...>
Nice, that's a good summary of the straw man arguments about the
"true" distinction between tuples and lists. Now can you please
explain why an "heterogene ous data structure":
1) does not support __setitem__, changing the value of an existing
item from 3 to 4,
2) supports iteration over its ("heterogeneneo us") elements, but not
an index() method, and
3) why using indices rather than names for implied semantics is a good
idea anyway.

As for addition/removal/insertion of elements not making sense for a
heterogeneous data structure, have you heard of database schema
change ?

Heterogeneous data structures are well known for several decades now;
they are commonly spelled "records" though, not tuples, and have a
more reasonable API to support their semantics.

George

Mar 1 '07 #8
Ben Finney wrote:
Bjoern Schliessmann <us************ **************@ spamgourmet.com >
>Explain.

Well, since you ask so politely :-)
I admit, sometimes I'm a little short-spoken ;)
>I know tuples as immutable lists ...

That's a common misconception.
[...]
Thanks for pointers, there's more to it than I suspected.

Regards,
Björn

--
BOFH excuse #384:

it's an ID-10-T error

Mar 1 '07 #9
George Sakkis, I agree with the things you say.
Sometimes you may have a sequence of uniform data with unknown len (so
its index doesn't have semantic meaning). You may want to use it as
dict key, so you probably use a tuple meant as just an immutable list.
I don't know Ruby, but I think it allows such purposes with a freezing
function.

Bye,
bearophile

Mar 1 '07 #10

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

Similar topics

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
11
3172
by: Ren | last post by:
Suppose I have a file containing several lines similar to this: :10000000E7280530AC00A530AD00AD0B0528AC0BE2 The data I want to extract are 8 hexadecimal strings, the first of which is E728, like this: :10000000 E728 0530 AC00 A530 AD00 AD0B 0528 AC0B E2 Also, the bytes in the string are reversed. The E728 needs to be 28E7,
7
1691
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 below. Does anyone know an easier way to accomplish this? Essentially, I want to avoid doing an 'eval' on a string to get it back into dict form... but still allow nested structures. (My current code doesn't handle nested structures.) I conked...
16
23015
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
18821
by: Amy L. | last post by:
I have a class that contains a string array. However, I can't get this object to serialize in the xml output. Is there a trick to get a string to serialize? Thanks Amy.
13
2720
by: Tony Girgenti | last post by:
Hello. Using VS.NET 2003 VB. If i have a string similar to the attached, how would i extract the "Truckname=" data from it in a loop and stay in the loop until the end of the string is reached ? As you can see the first truckname is "284165". The next truckname is "284193" Any help would be gratefully appreciated. Thanks,
6
2805
by: mandibdc | last post by:
I need to extract some elements from a very large XML file. Because of the size, I'd like to work with it on my Linux machine as a text file. Basically, I am going to have a list of specific strings I'm searching for. For each string, I need to search through the XML file, and when I find that string (in the tag <code>), copy the entire <item> XML element that the code appears in, into another text file. The XML document is comprised...
0
2049
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by word and number arguments like this: GRILLE EURAT5 Coin Nord-Ouest : 46.50/ 0.50 Coin Sud-E Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file.
3
3931
by: Daniel | last post by:
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...
0
9287
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
10046
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
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...
1
7259
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
6542
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
5155
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...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
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
3369
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.