[python-chinese] 如何从元组中提取元素
stin wang
stin.wang在gmail.com
星期四 八月 2 15:23:12 HKT 2007
ÎÒÊǸոտªÊ¼Ñ§Ï°python£¬ÆðÒòÊÇ´òËãÔÚwebgisÉÏÃæµ÷ÓûùÓÚpythonµÄÒ»¸ömatplotlib¿â¡£ÎÒ½øÐÐÁËһЩ³¢ÊÔ£¬Í¨¹ýpymssql¶ÁÈ¡mssql¿âÖеÄÄÚÈÝ£¬ÎÒÏ£ÍûÊǽ«Êý¾Ý¶Á³öÀ´ºó£¬Í¨¹ýmatplotlib»Ò»Ð©ÇúÏߺ͵ÈÖµÏß¡£²»¹ý´ÓÊý¾Ý¿âÈ¡»ØÀ´µÄÊý¾ÝÊÇÔª×éÀàÐ͵ģ¬plotµÄʱºòÐèÒªµÄÊÇlistÀàÐ͵ġ£
ÎÒ³¢ÊÔÁËһЩת»»
import _mssql
from pylab import *
mssql=_mssql.connect('172.18.23.102','user','user')
mssql.select_db('elementinfo')
query="EXEC sp_tables; SELECT SurfaceTemp FROM tabTimeData WHERE
(ObservTimes <= '2007041600') AND (ObservTimes >= '2007041500') and
(StationNum = '54517');"
for x in range(1):
if mssql.query(query):
header=mssql.fetch_array()
for y in header:
print y
((('TABLE_QUALIFIER', 1), ('TABLE_OWNER', 1), ('TABLE_NAME', 1),
('TABLE_TYPE', 1), ('REMARKS', 1)), 31, [('elementinfo', 'dbo',
'syscolumns', 'SYSTEM TABLE', None), ('elementinfo', 'dbo', 'syscomments',
'SYSTEM TABLE', None), ('elementinfo', 'dbo', 'sysdepends', 'SYSTEM TABLE',
None), ('elementinfo', 'dbo', 'sysfilegroups', 'SYSTEM TABLE', None),
('elementinfo', 'dbo', 'sysfiles', 'SYSTEM TABLE', None), ('elementinfo',
'dbo', 'sysfiles1', 'SYSTEM TABLE', None), ('elementinfo', 'dbo',
'sysforeignkeys', 'SYSTEM TABLE', None), ('elementinfo', 'dbo',
'sysfulltextcatalogs', 'SYSTEM TABLE', None), ('elementinfo', 'dbo',
'sysfulltextnotify', 'SYSTEM TABLE', None), ('elementinfo', 'dbo',
'sysindexes', 'SYSTEM TABLE', None), ('elementinfo', 'dbo', 'sysindexkeys',
'SYSTEM TABLE', None), ('elementinfo', 'dbo', 'sysmembers', 'SYSTEM TABLE',
None), ('elementinfo', 'dbo', 'sysobjects', 'SYSTEM TABLE', None),
('elementinfo', 'dbo', 'syspermissions', 'SYSTEM TABLE', None),
('elementinfo', 'dbo', 'sysproperties', 'SYSTEM TABLE', None),
('elementinfo', 'dbo', 'sysprotects', 'SYSTEM TABLE', None), ('elementinfo',
'dbo', 'sysreferences', 'SYSTEM TABLE', None), ('elementinfo', 'dbo',
'systypes', 'SYSTEM TABLE', None), ('elementinfo', 'dbo', 'sysusers',
'SYSTEM TABLE', None), ('elementinfo', 'dbo', 'dtproperties', 'TABLE',
None), ('elementinfo', 'dbo', 'sp', 'TABLE', None), ('elementinfo', 'dbo',
'tabAbsentInfo', 'TABLE', None), ('elementinfo', 'dbo', 'tabAlarmInfo',
'TABLE', None), ('elementinfo', 'dbo', 'tabDayData', 'TABLE', None),
('elementinfo', 'dbo', 'tabExtremum', 'TABLE', None), ('elementinfo', 'dbo',
'tabRealTimeData', 'TABLE', None), ('elementinfo', 'dbo', 'tabState',
'TABLE', None), ('elementinfo', 'dbo', 'tabTimeData', 'TABLE', None),
('elementinfo', 'dbo', 'tabTimeRadiation', 'TABLE', None), ('elementinfo',
'dbo', 'sysconstraints', 'VIEW', None), ('elementinfo', 'dbo',
'syssegments', 'VIEW', None)])
((('SurfaceTemp', 1),), 22, [('328 ',), ('284 ',), ('222 ',), ('204 ',),
('158 ',), ('115 ',), ('101 ',), ('97 ',), ('83 ',), ('70 ',), ('62 ',),
('53 ',), ('54 ',), ('48 ',), ('43 ',), ('36 ',), ('102 ',), ('144 ',),
('269 ',), ('285 ',), ('371 ',), ('385 ',)])
>>> type(header)
<type 'list'>
>>> type(header[0]
)
<type 'tuple'>
>>> print header[0]
((('TABLE_QUALIFIER', 1), ('TABLE_OWNER', 1), ('TABLE_NAME', 1),
('TABLE_TYPE', 1), ('REMARKS', 1)), 31, [('elementinfo', 'dbo',
'syscolumns', 'SYSTEM TABLE', None), ('elementinfo', 'dbo', 'syscomments',
'SYSTEM TABLE', None), ('elementinfo', 'dbo', 'sysdepends', 'SYSTEM TABLE',
None), ('elementinfo', 'dbo', 'sysfilegroups', 'SYSTEM TABLE', None),
('elementinfo', 'dbo', 'sysfiles', 'SYSTEM TABLE', None), ('elementinfo',
'dbo', 'sysfiles1', 'SYSTEM TABLE', None), ('elementinfo', 'dbo',
'sysforeignkeys', 'SYSTEM TABLE', None), ('elementinfo', 'dbo',
'sysfulltextcatalogs', 'SYSTEM TABLE', None), ('elementinfo', 'dbo',
'sysfulltextnotify', 'SYSTEM TABLE', None), ('elementinfo', 'dbo',
'sysindexes', 'SYSTEM TABLE', None), ('elementinfo', 'dbo', 'sysindexkeys',
'SYSTEM TABLE', None), ('elementinfo', 'dbo', 'sysmembers', 'SYSTEM TABLE',
None), ('elementinfo', 'dbo', 'sysobjects', 'SYSTEM TABLE', None),
('elementinfo', 'dbo', 'syspermissions', 'SYSTEM TABLE', None),
('elementinfo', 'dbo', 'sysproperties', 'SYSTEM TABLE', None),
('elementinfo', 'dbo', 'sysprotects', 'SYSTEM TABLE', None), ('elementinfo',
'dbo', 'sysreferences', 'SYSTEM TABLE', None), ('elementinfo', 'dbo',
'systypes', 'SYSTEM TABLE', None), ('elementinfo', 'dbo', 'sysusers',
'SYSTEM TABLE', None), ('elementinfo', 'dbo', 'dtproperties', 'TABLE',
None), ('elementinfo', 'dbo', 'sp', 'TABLE', None), ('elementinfo', 'dbo',
'tabAbsentInfo', 'TABLE', None), ('elementinfo', 'dbo', 'tabAlarmInfo',
'TABLE', None), ('elementinfo', 'dbo', 'tabDayData', 'TABLE', None),
('elementinfo', 'dbo', 'tabExtremum', 'TABLE', None), ('elementinfo', 'dbo',
'tabRealTimeData', 'TABLE', None), ('elementinfo', 'dbo', 'tabState',
'TABLE', None), ('elementinfo', 'dbo', 'tabTimeData', 'TABLE', None),
('elementinfo', 'dbo', 'tabTimeRadiation', 'TABLE', None), ('elementinfo',
'dbo', 'sysconstraints', 'VIEW', None), ('elementinfo', 'dbo',
'syssegments', 'VIEW', None)])
>>> print header[1]
((('SurfaceTemp', 1),), 22, [('328 ',), ('284 ',), ('222 ',), ('204 ',),
('158 ',), ('115 ',), ('101 ',), ('97 ',), ('83 ',), ('70 ',), ('62 ',),
('53 ',), ('54 ',), ('48 ',), ('43 ',), ('36 ',), ('102 ',), ('144 ',),
('269 ',), ('285 ',), ('371 ',), ('385 ',)])
>>>
ÎÒÏëÌáÈ¡header[1]µ±ÖеÄÕâЩÊý¾Ý£¬À´»Í¼£¬
>>> print header[1]
((('SurfaceTemp', 1),), 22, [('328 ',), ('284 ',), ('222 ',), ('204 ',),
('158 ',), ('115 ',), ('101 ',), ('97 ',), ('83 ',), ('70 ',), ('62 ',),
('53 ',), ('54 ',), ('48 ',), ('43 ',), ('36 ',), ('102 ',), ('144 ',),
('269 ',), ('285 ',), ('371 ',), ('385 ',)])
>>> type(header[1][2])
<type 'list'>
>>> print header[1][2]
[('328 ',), ('284 ',), ('222 ',), ('204 ',), ('158 ',), ('115 ',), ('101
',), ('97 ',), ('83 ',), ('70 ',), ('62 ',), ('53 ',), ('54 ',), ('48
',), ('43 ',), ('36 ',), ('102 ',), ('144 ',), ('269 ',), ('285 ',), ('371
',), ('385 ',)]
>>> print header[1][2][1]
('284 ',)
>>> type(header[1][2][1])
<type 'tuple'>
Ìá³öÀ´µÄÊýÈÔÈ»ÊÇÔª×飬²»ÖªµÀÔõô²ÅÄÜÌáÈ¡³ölist£¬listµ±ÖÐÒªÊÇ×Ö·û»òÕßÊý×ֵġ£
ÐÂÊÖÎÊÌ⣬´ó¼Ò²»ÒªÐ¦»°°¡£¬»¶ÓÓ»Ô¾·¢ÑÔ¡£
--
----------------------------------------------
Tie Wang
TianJin Climate Center
TianJin Meteorological Bureau
Tianjin, China, 300074
Email: stin.wang在gmail.com
Tel:+86-22-23331566-2377
----------------------------------------------
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070802/3dafca23/attachment-0001.htm
关于邮件列表 python-chinese 的更多信息