[新手]列表中的奇怪输出 [英] [Newbie] Strange output from list
问题描述
你好
从SQLite
数据库中选择数据时,我得到了一些不需要的结果:
======
sql =''SELECT id FROM master''
rows = list(cursor.execute(sql))
$行中id的b $ b:
sql =''SELECT COUNT(代码)FROM公司WHERE code ="%s"''%id [0]
result = list(cursor.execute(sql))
print" Code =%s,number =%s" %(id [0],结果[0])
======
代码= 0111Z,数字=(47,)
======
我希望看到number = 47。为什么Python返回"(47,)"?
谢谢。
Gilles Ganault< no **** @ nospam.com写道:
你好
我得到了一些不必要的结果当从SQLite
数据库中选择数据时:
======
sql =''SELECT id FROM master ''
rows = list(cursor.execute(sql))
行中id的
:
sql =''SELECT COUNT(代码)FROM公司WHERE code ="%s"''%id [0]
result = list(cursor.execute(sql))
print" Code = %s,number =%s" %(id [0],结果[0])
======
代码= 0111Z,数字=(47,)
======
我希望看到number = 47。为什么Python返回(47,)?
SQL SELECT的结果是一系列元组,其中元组中的每个项目
都是指定列的值在SELECT
子句中。
SQLAlchemy用一系列ResultProxy对象表示它。
将ResultProxy对象转换为一个字符串,它显示为
元组。有关访问ResultProxy对象的各种
属性的其他方法,请参阅文档。
-
\ a ??是什么让一个完全陌生的人潜入冰冷的河流中拯救一个纯金宝宝?也许我们永远不会知道。 a ??杰克|
_o__)Handey |
Ben Finney
道歉,我的回答相当困惑。
Ben Finney< bi **************** @ benfinney.id.auwrites:
SQL SELECT的结果是一系列元组,其中元组中的每个项目
是SELECT中指定的列的值
条款。
这仍然是真的。无论你在
SELECT子句中指定了多少列,每个结果行都是一个元组。
SQLAlchemy用一个序列表示ResultProxy对象。
我错误地认为你正在使用SQLAlchemy,重新阅读
你的帖子看起来不太可能。
相反,通过标准库a ?? sqlite3a ??模块,你将收到
每个结果行作为一个?? sqlite3.Rowa ??对象:
一个Row实例用作
连接对象的高度优化的row_factory。它试图模仿其大部分
功能中的元组。
它支持按列名和索引进行映射访问,迭代,
表示,相等测试和len()。
< URL:http://docs.python.org/library/sqlite3.html#row-objects>
由于您只要求打印行,因此您获得了整行的
字符串表示(模仿Python元组,
但实际上是一个具有更多功能的不同类。)
-
\ a ??极客认为他们可以忽视政治。你可以|
` \只留下政治,但政治不会让你孤单。 |
_o__)a ?? Richard Stallman,2002-07-26 |
Ben Finney
Ben Finney写道:
Gilles Ganault< no **** @ nospam.comwrites:
> Hello
从SQLite数据库中选择数据时,我得到了一些不需要的结果:
======
sql =''SELECT id FROM master''
rows = list(cursor.execute(sql))
对于行中的id:
sql =''SELECT COUNT(code)FROM companies WHERE code ="%s"''%id [0]
result = list(cursor.execute(sql))
print" Code =%s,number =%s" %(id [0],结果[0])
======
代码= 0111Z,数字=(47,)
======
我希望看到number = 47。为什么Python返回"(47,)"?
SQL SELECT的结果是一系列元组,其中每个项目
$ b元组中的$ b是SELECT
子句中指定的列的值。
SQLAlchemy用一系列ResultProxy对象表示它。 />
将ResultProxy对象转换为字符串时,它显示为
元组。有关访问ResultProxy对象的各种
属性的其他方法,请参阅文档。
(47,)是一个Python的表示形式项目元组
如果你想:
代码= 0111Z,数字= 47
只需将你的代码更改为:
sql =''SELECT id FROM master''
rows = list(cursor.execute(sql))
表示行中的id:
sql =''SELECT COUNT(代码)FROM公司WHERE code ="%s"''%id [0]
result = list(cursor.execute(sql))
print" Code =%s,number =%s" %(id [0],result [0] [0])
注意结果上的额外[0]索引
英文:
结果项目零的元组项目零点
Eg
>> result = [(47,)]
result =结果[0]
结果
(47,)
< blockquote class =post_quotes>
>> result [0]
47
-
Andrew
Hello
I''m getting some unwanted result when SELECTing data from an SQLite
database:
======
sql = ''SELECT id FROM master''
rows=list(cursor.execute(sql))
for id in rows:
sql = ''SELECT COUNT(code) FROM companies WHERE code="%s"'' % id[0]
result = list(cursor.execute(sql))
print "Code=%s, number=%s" % (id[0],result[0])
======
Code=0111Z, number=(47,)
======
I expected to see "number=47". Why does Python return "(47,)"?
Thank you.
Gilles Ganault <no****@nospam.comwrites:
Hello
I''m getting some unwanted result when SELECTing data from an SQLite
database:
======
sql = ''SELECT id FROM master''
rows=list(cursor.execute(sql))
for id in rows:
sql = ''SELECT COUNT(code) FROM companies WHERE code="%s"'' % id[0]
result = list(cursor.execute(sql))
print "Code=%s, number=%s" % (id[0],result[0])
======
Code=0111Z, number=(47,)
======
I expected to see "number=47". Why does Python return "(47,)"?The result of an SQL SELECT is a sequence of tuples, where each item
in the tuple is a value for a column as specified in the SELECT
clause.
SQLAlchemy represents this with a sequence of ResultProxy objects.
When you convert a ResultProxy object to a string, it displays like a
tuple. See the documentation for other ways of accessing various
attributes of a ResultProxy object.
--
\ a??What is it that makes a complete stranger dive into an icy |
`\ river to save a solid gold baby? Maybe we''ll never know.a?? a??Jack |
_o__) Handey |
Ben Finney
My apologies, my response was rather confused.
Ben Finney <bi****************@benfinney.id.auwrites:
The result of an SQL SELECT is a sequence of tuples, where each item
in the tuple is a value for a column as specified in the SELECT
clause.This remains true. No matter how many columns you specify in the
SELECT clause, each result row is a tuple.
SQLAlchemy represents this with a sequence of ResultProxy objects.I mistakenly assumed you are using SQLAlchemy, which on re-reading
your post doesn''t seem likely.
Instead, by the standard library a??sqlite3a?? module, you will receive
each result row as an a??sqlite3.Rowa?? object:
A Row instance serves as a highly optimized row_factory for
Connection objects. It tries to mimic a tuple in most of its
features.
It supports mapping access by column name and index, iteration,
representation, equality testing and len().
<URL:http://docs.python.org/library/sqlite3.html#row-objects>
Since you only asked for the row to be printed, you therefore got a
string representation of the entire row (which mimics a Python tuple,
but is actually a different class with more functionality).
--
\ a??Geeks like to think that they can ignore politics. You can |
`\ leave politics alone, but politics won''t leave you alone.a?? |
_o__) a??Richard Stallman, 2002-07-26 |
Ben Finney
Ben Finney wrote:Gilles Ganault <no****@nospam.comwrites:
>Hello
I''m getting some unwanted result when SELECTing data from an SQLite
database:
======
sql = ''SELECT id FROM master''
rows=list(cursor.execute(sql))
for id in rows:
sql = ''SELECT COUNT(code) FROM companies WHERE code="%s"'' % id[0]
result = list(cursor.execute(sql))
print "Code=%s, number=%s" % (id[0],result[0])
======
Code=0111Z, number=(47,)
======
I expected to see "number=47". Why does Python return "(47,)"?
The result of an SQL SELECT is a sequence of tuples, where each item
in the tuple is a value for a column as specified in the SELECT
clause.
SQLAlchemy represents this with a sequence of ResultProxy objects.
When you convert a ResultProxy object to a string, it displays like a
tuple. See the documentation for other ways of accessing various
attributes of a ResultProxy object.
(47,) is the python representation of a one item tuple
If you want:
Code=0111Z, number=47
Just change your code to:
sql = ''SELECT id FROM master''
rows=list(cursor.execute(sql))
for id in rows:
sql = ''SELECT COUNT(code) FROM companies WHERE code="%s"'' % id[0]
result = list(cursor.execute(sql))
print "Code=%s, number=%s" % (id[0],result[0][0])
Notice the extra [0] index on the "result"
In English:
Item zero of the tuple that is item zero of result
E.g.>>result = [(47,)]
result = result[0]
result
(47,)
>>result[0]
47
--
Andrew
这篇关于[新手]列表中的奇怪输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!