在Python和Oracle 11g中从cur.fetchall()下载数组

在Python和Oracle 11g中从cur.fetchall()下载数组,python,sql,oracle11g,tuples,fetchall,Python,Sql,Oracle11g,Tuples,Fetchall,我正在尝试使用cur.fetchall命令将Oracle 11g中的单个数组下载到Python中。我使用以下语法: con = cx_Oracle.connect('xxx') print con.version cur = con.cursor() cur.execute("select zc.latitude from orders o, zip_code zc where o.date> '24-DEC-12' and TO_CHAR(zc.ZIP_CODE)=o.P

我正在尝试使用cur.fetchall命令将Oracle 11g中的单个数组下载到Python中。我使用以下语法:

 con = cx_Oracle.connect('xxx')
 print con.version
 cur = con.cursor()
cur.execute("select zc.latitude from  orders o, zip_code zc where o.date> '24-DEC-12'     and TO_CHAR(zc.ZIP_CODE)=o.POSTAL_CODE")
latitudes = cur.fetchall()
cur.close()
print latitudes
当我打印纬度时,我得到:

[(-73.98353999999999,), (-73.96565,), (-73.9531,),....]
问题在于,当我试图操纵数据时——在本例中,通过:

x,y = map(longitudes,latitudes)
我得到了以下错误--注意,我正在使用相同类型的语法来创建“经度”:

TypeError: a float is required
我怀疑这是因为cur.fetchall正在返回元组元素中带有逗号的元组。如何运行查询以避免在括号内获取逗号,并获取数组而不是元组?有没有像cur.fetchall这样的“catch all”命令,或者我必须手动循环才能将结果放入数组中

我的完整代码如下:

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import cx_Oracle
con = cx_Oracle.connect('xxx')
print con.version
cur = con.cursor()
cur.execute("select zc.latitude from  orders o, zip_code zc where     psh.ship_date> '24-DEC-12' and TO_CHAR(zc.ZIP_CODE)=o.CONSIGNEE_POSTAL_CODE")
latitudes = cur.fetchall()
cur.close()

cur = con.cursor()
cur.execute("select zc.longitude from  orders o, zip_code zc where psh.ship_date> '24-DEC-12' and TO_CHAR(zc.ZIP_CODE)=o.CONSIGNEE_POSTAL_CODE")
longitudes = cur.fetchall()
print 'i made it!'
print latitudes
print longitudes
cur.close()
con.close()
map = Basemap(resolution='l',projection='merc',         llcrnrlat=25.0,urcrnrlat=52.0,llcrnrlon=-135.,urcrnrlon=-60.0,lat_ts=51.0)
# draw coastlines, country boundaries, fill continents.
map.drawcoastlines(color ='C')
map.drawcountries(color ='C')
map.fillcontinents(color ='k')
# draw the edge of the map projection region (the projection limb)
map.drawmapboundary()
# draw lat/lon grid lines every 30 degrees.
map.drawmeridians(np.arange(0, 360, 30))
map.drawparallels(np.arange(-90, 90, 30))
plt.show()
# compute the native map projection coordinates for the orders.
x,y = map(longitudes,latitudes)
# plot filled circles at the locations of the orders.
map.plot(x,y,'yo')

尾随逗号很好,这是有效的元组语法,也是打印元组时得到的

我不知道你们想要实现什么,但地图可能不是你们想要的。map接受一个函数和一个列表作为参数,但您给它两个列表。更有用的方法可能是从数据库中同时检索纬度和经度:

cur.execute("select zc.longitude, zc.latitude from  orders o, zip_code zc where o.date> '24-DEC-12'     and TO_CHAR(zc.ZIP_CODE)=o.POSTAL_CODE")
评论的最新情况 从原始代码看,您似乎正在尝试使用内置的
map
功能,而更新后的代码则不是这样

获取
TypeError
的原因是matplotlib需要一个浮点列表,但您提供的是一个元组列表。您可以通过简单的列表理解从原始纬度展开元组(内置的地图也可以做到这一点):

使用一个查询返回纬度和经度:

cur.execute("select zc.longitude, zc.latitude from...")
points = cur.fetchall()
longitudes = [point[0] for point in longitudes]
latitudes = [point[1] for point in latitudes]

现在,
经度
纬度
是浮点数列表。

尾随逗号很好,这是有效的元组语法,是打印元组时得到的

我不知道你们想要实现什么,但地图可能不是你们想要的。map接受一个函数和一个列表作为参数,但您给它两个列表。更有用的方法可能是从数据库中同时检索纬度和经度:

cur.execute("select zc.longitude, zc.latitude from  orders o, zip_code zc where o.date> '24-DEC-12'     and TO_CHAR(zc.ZIP_CODE)=o.POSTAL_CODE")
评论的最新情况 从原始代码看,您似乎正在尝试使用内置的
map
功能,而更新后的代码则不是这样

获取
TypeError
的原因是matplotlib需要一个浮点列表,但您提供的是一个元组列表。您可以通过简单的列表理解从原始纬度展开元组(内置的地图也可以做到这一点):

使用一个查询返回纬度和经度:

cur.execute("select zc.longitude, zc.latitude from...")
points = cur.fetchall()
longitudes = [point[0] for point in longitudes]
latitudes = [point[1] for point in latitudes]

现在,
经度
纬度
是浮动的列表。

找到了另一种解决方法——查看。谢谢你努力工作帮助我

找到了解决这个问题的另一种方法——看看。谢谢你努力工作帮助我

我也遇到了同样的问题,其中一个解决方法是清除循环中元组元素中的逗号,正如您所提到的(虽然效率不高),类似这样:

cleaned_up_latitudes=[] #initialize the list
for x in xrange(len(latitudes)):
    pass
cleaned_up_latitudes.append(latitudes[x][0]) #add first element
print cleaned_up_latitudes
[-73.983539999999,-73.96565,-73.9531,…]


我希望这会有所帮助。

我也遇到了同样的问题,解决方法之一就是像您提到的那样(虽然效率不高),从循环中的元组元素中清除逗号,如下所示:

cleaned_up_latitudes=[] #initialize the list
for x in xrange(len(latitudes)):
    pass
cleaned_up_latitudes.append(latitudes[x][0]) #add first element
print cleaned_up_latitudes
[-73.983539999999,-73.96565,-73.9531,…]


我希望这能有所帮助。

为了进一步解释,我正在用基线地图绘制美国地图上的纬度和经度。这是我的全部代码:我把我的全部代码放在上面,但基本上我试图从地理上划分订单。我在过去通过x,y=map(array1,array2)将两个数组传递到map()中没有问题,所以我怀疑这不是问题所在。如果我错了,请纠正我。如果您认为同时检索纬度和经度更有意义,您能告诉我如何将其传递到map()和map.plot()中吗?我逐字复制了您的代码,但当我打印出“纬度”和“经度”时,我得到了:我逐字复制了您的代码,但当我打印出“纬度”和“经度”时,我得到:[],即数组未填充。我错过什么了吗?我必须将纬度和经度定义为数组以避免错误:名称未定义。下面是新代码:导入cx_Oracle con=cx_Oracle.connect('xxx')cur=con.cursor()cur.execute(“选择zc.latitude,zc.longitude…”)points=cur.fetchall()经度=[]经度=[]经度=[经度点[0]表示经度]经度=[经度点[1]表示经度]cur.close()con.close()为了进一步解释,我使用Basemap在美国地图上绘制纬度和经度。这是我的全部代码:我把我的全部代码放在上面,但基本上我试图从地理上划分订单。我在过去通过x,y=map(array1,array2)将两个数组传递到map()中没有问题,所以我怀疑这不是问题所在。如果我错了,请纠正我。如果您认为同时检索纬度和经度更有意义,您能告诉我如何将其传递到map()和map.plot()中吗?我逐字复制了您的代码,但当我打印出“纬度”和“经度”时,我得到了:我逐字复制了您的代码,但当我打印出“纬度”和“经度”时,我得到:[],即数组未填充。我错过什么了吗?我必须将纬度和经度定义为数组以避免错误:名称未定义。下面是新代码:导入cx_Oracle con=cx_Oracle.connect('xxx')cur=con.cursor()cur.execute(“选择zc.latitude,zc.longitude…”)points=cur.fetchall()经度=[]经度=[]经度=[经度点[0]表示经度]经度=[经度点[1]表示经度]cur.close()con.close()