mi programa Python está conectado a una base de datos postgresql. Mi objetivo es crear un archivo xml en formato VOTable con el resultado de una consulta en la base de datos. Pero estoy atascado. Ahí está el código de la creación del archivo:
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])Eso funciona porque es un caso específico porque sé que habrá 7 columnas en el resultado de mi consulta, por eso puse:
table.array[i]=(dataRes[0],dataRes[1],dataRes[2],dataRes[3],dataRes[4],dataRes[5],dataRes[6])Pero quiero algo más general, ¿cómo puedo crear mi tabla. matriz sin saber el número de columna en el resultado de mi consulta?
Intento algo como esto pero no funciona:
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+=1También intento en el ejemplo hacer:
table.array[0][0]=dataRes[0] table.array[0][0]=dataRes[1]Pero tampoco funciona
el error es:
AttributeError: el objeto 'NoneType' no tiene atributo 'decode'
Gracias por tu ayuda