Python:使用astropy.io.votab

2024-04-24 06:48:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我的Python程序连接到postgresql数据库。 我的目标是在数据库上创建一个VOTable格式的xml文件。但我被卡住了。有创建文件的代码:

from astropy.io.votable import parse,is_votable
from astropy.io.votable.tree import VOTableFile, Resource, Table, Field
import sys
import psycopg2

cur.execute("""%s"""%queryString)
#Number of row
rowNumber = cur.rowcount
#Number of column
columnNumber=len(cur.description)


#Create votable file
votable = VOTableFile()
resource = Resource()
votable.resources.append(resource)
table = Table(votable)
table.name="Answer"
resource.tables.append(table)

# Create field
for column in dataCol:
    table.fields.extend([
            Field(votable, name=column, datatype="char", arraysize="*")])

table.create_arrays(rowNumber)
i=0
for row in cur:
    dataRes = []

    # if a row have a no value column, replace by ''
    for datas in row:
        if datas is None:
            dataRes.append('')
        else:
            dataRes.append(datas)

    table.array[i]=(dataRes[0],dataRes[1],dataRes[2],dataRes[3],dataRes[4],dataRes[5],dataRes[6])

这是有效的因为这是一个特定的案例,因为我知道我的查询结果中将有7列,所以我把:

^{pr2}$

但是我想要更一般的东西,我怎么能创造我的表.数组不知道查询结果中的列数?在

我试过这样的方法,但没用:

  votable = VOTableFile()
    resource = Resource()
    votable.resources.append(resource)
    table = Table(votable)
    table.name="Answer"
    resource.tables.append(table)

# Create field
for column in dataCol:
    table.fields.extend([
            Field(votable, name=column, datatype="char", arraysize="*")])

table.create_arrays(rowNumber)
i=0
for row in cur:
    dataRes = []

# if a row have a no value column, replace by ''
for datas in row:
    if datas is None:
        dataRes.append('')
    else:
        dataRes.append(datas)

for column in dataRes:
    table.array[i][j]=column[j]
    print(i,j)
    j+=1
i+=1

我还试着举例说明:

table.array[0][0]=dataRes[0]
table.array[0][0]=dataRes[1]

不管用,但也不行

错误是:

AttributeError:“NoneType”对象没有属性“decode”

谢谢你的帮助


Tags: nameinimportforiftablecolumnarray