0

TypeError: cannot serialize ('John',) (type tuple)在第一次迭代中得到了这段代码。为什么SELECT name FROM potluck输出'John',而不是输出'John'

('John',)
('Sandy',)
('Tom',)
('Tina',)

反正我不知道是不是这个问题。


#!/usr/bin/env python
import psycopg2
import sys

from xml.etree.ElementTree import Element, SubElement, Comment, tostring

con = None

try:

    con = psycopg2.connect(database='events', user='demo')

    cur = con.cursor()
    cur.execute("SELECT name FROM potluck")

    rows = cur.fetchall()

    top = Element('top')
    for row in rows:
        #print row
        comment = Comment('Generated test')
        top.append(comment)

        child = SubElement(top, 'child')
        child.text = row
        print tostring(top)

except psycopg2.DatabaseError, e:
    print 'Error %s' % e
    sys.exit(1)


finally:

    if con:
        con.close()
4

1 回答 1

1

尝试这个:

child.text = row[0]

元素的文本字段通常是单个字符串。

参考:http ://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.text

于 2013-10-01T00:06:35.460 回答