473,770 Members | 2,144 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
12 4167
On 2007-02-28, at************* @gmail.com <at************ *@gmail.comwrot e:
>>>import itertools
tuple(iterto ols.chain((t[0], t2[0].encode('ascii' )), t[2:]))
('eco', 'Roads', 0.0739698873013 48305)
Steven,

As suggested in the previous article, I handled it where the values are
read from the list retrieved from the database. By adding an additional
index of [0] the format is correct.

Thank you all very much,

Rich
Mar 1 '07 #11
On Mar 1, 5:02 am, bearophileH...@ lycos.com wrote:
I don't know Ruby, but I think it allows such purposes with a freezing
function.
In ruby all objects can be frozen (freeze is a method on Object, from
which all other objects derive), not just Arrays (Arrays == lists in
python; ruby has no built-in container equiv. to tuple). But that's
more of an implementation detail rather than anthing to do with the
structure/semantics of a certain type of object (e.g., a String can be
frozen, a Hash can be frozen, &c).

Regards,
Jordan

Mar 1 '07 #12
"George Sakkis" <ge***********@ gmail.comwrites :
On Feb 28, 10:45 pm, Ben Finney wrote:
Tuples are intended for use as heterogeneous data structures [...]
Lists are intended for use as homogeneous sequences [...]

Nice, that's a good summary of the straw man arguments about the
"true" distinction between tuples and lists.
I'm not sure why you say it's a "straw man argument". I'm presenting
*my* understanding of a position that I also share, in order to defend
it; a straw man argument is a misrepresentati on of *another party's*
position for the purpose of appearing to attack that party's position.

http://www.fallacyfiles.org/strawman.html

Whose position have I misrepresented and attacked?
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,
In the case of a tuple, because the "value" is conceptually the entire
tuple. To change one of its items would be to create a new value -- so
that's what is supported.
2) supports iteration over its ("heterogeneneo us") elements, but not
an index() method
An index() method would imply that the index of an item has some
meaning, such that extracting a single item is meaningful. Since a
tuple represents a single conceptual structural value, to extract one
item is something to be done at the same time as extracting all the
others.
3) why using indices rather than names for implied semantics is a
good idea anyway.
You've already shown that one *doesn't* use an index for accessing
items in a tuple.
As for addition/removal/insertion of elements not making sense for a
heterogeneous data structure, have you heard of database schema
change ?
A database schema change is not an operation one performs with the
expectation that the tuples will remain the same. Thus, one would
expect to discard the old tuples as obsolete and retrieve them from
the relation again, getting new tuples.
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.
Python doesn't natively support relational schema operations. It does
natively support tuples. I never professed that the two were the same,
and don't accept that they should be.

--
\ "I have never made but one prayer to God, a very short one: 'O |
`\ Lord, make my enemies ridiculous!' And God granted it." -- |
_o__) Voltaire |
Ben Finney

Mar 1 '07 #13

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
3174
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
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
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
2721
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
2050
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
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
10260
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
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...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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...
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();...
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.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.