从Python中的Numpy数组获取SQL头

发布于 2024-11-05 08:16:26 字数 345 浏览 1 评论 0原文

通过下面的内容,我可以从 SQL 中获取行和列数据:
如何获取表头作为结果集或数组的一部分?

    top = csr.execute("Select * from bigtop")
    d=list(top)
    a = np.asarray(d, dtype='object')
    print a

就像我在这里问的: 如何从数据库创建 CSV 文件Python?

With below, I can get the row and col data from SQL:
How would I get the table headers as part of the result set or array.?

    top = csr.execute("Select * from bigtop")
    d=list(top)
    a = np.asarray(d, dtype='object')
    print a

Just like I asked here:
How do I create a CSV file from database in Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风轻花落早 2024-11-12 08:16:26

这是一个独立的示例,说明了总体思路。 numpy.recarray 是你的朋友,

from sqlite3 import connect
from numpy import asarray

db = connect(":memory:")
c = db.cursor()
c.execute('create table bigtop (a int, b int, c int)')

for v in [(1,2,3),(4,5,6),(7,8,9)]:
    c.execute('insert into bigtop values (?,?,?)',v)

s = c.execute('select * from bigtop')

h = [(i[0],int) for i in c.description]

# You can also use 'object' for your type
# h = [(i[0],object) for i in c.description]

a = asarray(list(s),dtype=h)

print a['a']

给出第一列,

[1 4 7]

print a.dtype

给出每列的名称和类型,

[('a', '<i4'), ('b', '<i4'), ('c', '<i4')]

或者,如果你使用 object 作为你的类型,你会得到,

[('a', '|O4'), ('b', '|O4'), ('c', '|O4')]

This is a self contained example that illustrates the general idea. numpy.recarray is your friend,

from sqlite3 import connect
from numpy import asarray

db = connect(":memory:")
c = db.cursor()
c.execute('create table bigtop (a int, b int, c int)')

for v in [(1,2,3),(4,5,6),(7,8,9)]:
    c.execute('insert into bigtop values (?,?,?)',v)

s = c.execute('select * from bigtop')

h = [(i[0],int) for i in c.description]

# You can also use 'object' for your type
# h = [(i[0],object) for i in c.description]

a = asarray(list(s),dtype=h)

print a['a']

gives the first column,

[1 4 7]

and,

print a.dtype

gives the name and type of each column,

[('a', '<i4'), ('b', '<i4'), ('c', '<i4')]

alternatively, if you used object as your type, you get,

[('a', '|O4'), ('b', '|O4'), ('c', '|O4')]
无所的.畏惧 2024-11-12 08:16:26

csr.description 应该有标题

csr.description should have the headers

帝王念 2024-11-12 08:16:26

如果您希望列名称作为数组中的第一行,您会

top = csr.execute("Select * from bigtop")
d=list(top)
a = np.asarray([[x[0] for x in top.description]] + d, dtype='object')

得到类似的结果

array([[heading1, heading2, heading3, ...],
       [val1, val2, val3, ...],
           ...
           , dtype=object)

If you wanted the column names as the first line in your array, you would do

top = csr.execute("Select * from bigtop")
d=list(top)
a = np.asarray([[x[0] for x in top.description]] + d, dtype='object')

and get something like

array([[heading1, heading2, heading3, ...],
       [val1, val2, val3, ...],
           ...
           , dtype=object)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文