my Python program is connect to a postgresql data base. My goal is to create a xml file at the VOTable format with the result of a query on the data base. But I am stuck. There is the code of the creation of the file:
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])
Thats work because it is a specific case because I know there will be 7 columns in my query result, that's why I put:
table.array[i]=(dataRes[0],dataRes[1],dataRes[2],dataRes[3],dataRes[4],dataRes[5],dataRes[6])
But I want something more general, how can I create my table.array without to know the number of column in my query result ?
I try something like this but it's doesn't work:
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
I also try in example to make:
table.array[0][0]=dataRes[0]
table.array[0][0]=dataRes[1]
But it's doesn't work too
The error is:
AttributeError: 'NoneType' object has no attribute 'decode'
Thank you for your help