从封闭元组中提取字符串 [英] Extract String From Enclosing Tuple

查看:92
本文介绍了从封闭元组中提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点不好意思就此请求帮助,但我找不到

我在这里的文档中的解决方案。


组装数据以写入数据库表。代表元组

看起来像这样:


(''eco'','(u''Roads'',)",0.073969887301348305)


Pysqlite不喜欢中期格式:

pysqlite2.dbapi2.InterfaceError:错误绑定参数1 - 可能是

不支持的类型。


我想提取''Roads'',部分来自双引号封闭的

元组。当字符串打印时,应该自动删除unicode部分,但我怀疑它是双引号和额外的括号,这是问题的原因。我知道元组是不可变的,但我认为我可以将b $ b切成片。如果是这样,我没有正确地做到这一点,因为每次尝试都会导致

TypeError:unsubscriptable对象


即使我将中间项分配给a在组装

元组之前插入数据库中的变量,我只是无法完成工作。无论是在三个元组的元组中还是单独的
,我都没有应用正确的

技术。


Insight赞赏。


Rich

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.073969887301348305)

Pysqlite doesn''t like the format of the middle term:
pysqlite2.dbapi2.InterfaceError: 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

推荐答案

rs ****** @ nospam.appl-ecosys.com écrit:
rs******@nospam.appl-ecosys.com a écrit :

I' 我有点不好意思就此请求帮助,但我找不到

我在这里的文档中的解决方案。


数据组装用于写入数据库表。代表元组

看起来像这样:


(''eco'','(u''Roads'',)",0.073969887301348305)


Pysqlite不喜欢中期格式:

pysqlite2.dbapi2.InterfaceError:错误绑定参数1 - 可能是

不支持的类型。


我想提取''Roads'',部分来自双引号封闭的

元组。当字符串打印时,应该自动删除unicode部分,但我怀疑它是双引号和额外的括号,这是问题的原因。我知道元组是不可变的,但我认为我可以将b $ b切成片。如果是这样,我没有正确地做,因为每次尝试都会导致

TypeError:unsubscriptable对象
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.073969887301348305)

Pysqlite doesn''t like the format of the middle term:
pysqlite2.dbapi2.InterfaceError: 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



你从哪里得到你的数据来自? MHO是你最好在源头处理

问题(即:不要在数据中放置元组的字符串表示

)而不是试图修复之后。

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.


2月28日下午12:40,rshep ... @nospam.appl-ecosys.com写道:
On Feb 28, 12:40 pm, rshep...@nospam.appl-ecosys.com wrote:

我有点不好意思就此请求帮助,但我找不到

我在这里的文档中的解决方案。


组装数据以写入数据库表。代表元组

看起来像这样:


(''eco'','(u''Roads'',)",0.073969887301348305)


Pysqlite不喜欢中期格式:

pysqlite2.dbapi2.InterfaceError:错误绑定参数1 - 可能是

不支持的类型。


我想提取''Roads'',部分来自双引号封闭的

元组。
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.073969887301348305)

Pysqlite doesn''t like the format of the middle term:
pysqlite2.dbapi2.InterfaceError: 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.073969887301348305)
t2 = eval(t [1],{''__ builtin ___'':{}},{})
t2
>>t = (''eco'', "(u''Roads'',)", 0.073969887301348305)
t2 = eval(t[1], { ''__builtins__'' : {} }, {} )
t2



(u''Roads'',)

(u''Roads'',)


>> t2 [0 ] .encode(''ascii'')
>>t2[0].encode(''ascii'')



''Roads''

''Roads''


>> import itertools
tuple(itertools.chain((t [0],t2 [0 ] .encode(''ascii'')),t [2:]))
>>import itertools
tuple(itertools.chain((t[0], t2[0].encode(''ascii'')), t[2:]))



(''eco'', ''Roads'',0.0739698873013483 05)

(''eco'', ''Roads'', 0.073969887301348305)


>> tuple(itertools.chain((t [0], (t2 [0] .encode(''ascii''),)),t [2:]))
>>tuple(itertools.chain((t[0], (t2[0].encode(''ascii''),)), t[2:]))



( ''eco'',(''Roads'',),0.073969887301348305)

-

希望这会有所帮助,

Steven

(''eco'', (''Roads'',), 0.073969887301348305)
--
Hope this helps,
Steven


rs ****** @ nospam .appl-ecosys.com 写道:
rs******@nospam.appl-ecosys.com writes:

组装数据以写入数据库表。一个

代表元组如下所示:


(''eco'','(u''Roads'',)",0.073969887301348305)
Data are assembled for writing to a database table. A
representative tuple looks like this:

(''eco'', "(u''Roads'',)", 0.073969887301348305)



您将第二项称为一元组后来,但事实并非如此;它现在只是一个字符串(甚至不是unicode字符串)。无论是什么组装了

这些都有效地丢失了数据的结构,你现在还剩下三个项目在元组中:字符串,字符串,浮点数。


有些RDBMS可以在一个值中存储多个值的结构;

SQLite不能。这个限制的通常解决方案是采用这些

结构值并将组件值存储为

不同表的单独行,并将每个行引用回

标识原始表的关键字,以便它们可以轻松连接。


例如,我可能在概念上将订单记录视为以下

元组,每个订单上的项目还有元组元组:


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 ))),




因为我不能在SQLite中存储那些,我需要重组

表格:分开订单商品在专用的

表中分隔行,并参考订单表。每个订单商品中的表格键行。


orders =(

#fields:id,cust_code,date

(1," cust1234"," ; 2007-02-15"),

(2,cust4567,2007-02-19),




order_items =(

#fields:id,order_id,item_code,数量

(1,1," item002",1),

(2,1," item005",3),

(3,1," item007",1),

(4) ,2," item001",5),

(5,2," item005",2),




然后你可以根据需要使用SQL JOIN子句,将

order_item.order_id字段作为订单表中的外键。


-

\我搬进了一个全电动的房子。我忘记了,整天都离开了|

` \门廊灯。当我回到家时,前门不会打开。 - 史蒂芬赖特|

Ben Finney

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.order_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


这篇关于从封闭元组中提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆