【问题标题】:Downloading arrays off cur.fetchall() in Python and Oracle 11g在 Python 和 Oracle 11g 中从 cur.fetchall() 下载数组
【发布时间】:2012-12-26 20:23:03
【问题描述】:

我正在尝试使用 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')

【问题讨论】:

    标签: python sql oracle11g tuples fetchall


    【解决方案1】:

    结尾的逗号很好,它是有效的元组语法以及当您 print 元组时得到的内容。

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

    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 需要一个浮点列表,但您提供的是一个元组列表。您可以通过简单的列表理解从原始纬度中解开元组(内置的地图也可以解决问题):

    [row[0] for row in latitudes]
    

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

    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]
    

    现在longitudeslatitudes 是浮点数列表。

    【讨论】:

    • 为了进一步解释,我正在使用 Basemap 在美国地图上绘制纬度和经度。这是我的全部代码:
    • 我已经把我的整个代码放在上面了,但基本上我是在尝试按地理位置绘制订单。过去我通过 x,y=map(array1, array2) 将两个数组传递给 map() 没有问题,所以我怀疑这不是问题。如果我错了,请纠正我。如果您认为将纬度和经度一起检索更有意义,您能否说明我如何将其传递给 map() 然后 map.plot()?
    • 我逐字复制了你的代码,但是当我打印出“纬度”和“经度”时,我得到:
    • 我逐字复制了您的代码,但是当我打印出“纬度”和“经度”时,我得到:[],即未填充数组。我错过了什么吗?我必须将纬度和经度定义为数组以避免错误:名称未定义。这是新代码: import cx_Oracle con = cx_Oracle.connect('xxx') cur = con.cursor() cur.execute("select zc.latitude, zc.longitude...") points = cur.fetchall() longitudes =[] latitudes=[] longitudes = [point[0] for point in longitudes] latitudes = [point[1] for point in latitudes] cur.close() con.close()
    【解决方案2】:

    找到了解决这个问题的另一种方法——查看TypeError: "list indices must be integers" looping through tuples in python。感谢您的辛勤工作帮助我!

    【讨论】:

      【解决方案3】:

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

      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.98353999999999, -73.96565, -73.9531,....]

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-21
        • 2011-07-08
        • 2016-12-05
        • 1970-01-01
        相关资源
        最近更新 更多