好吧,@Jon Clements提供的一个解决方案是:
c = connection.cursor()
c.execute("SELECT * FROM my_table")
rows = (str(col) if col is not None else None for col in c.fetchall())
for row in rows:
print (row)
我的最终解决方案是:
c.execute("SELECT * FROM my_table LIMIT 1")
select_sql = "SELECT "
for row in c.description:
column_name = row.name
if type_ == 17: #binary data
select_sql += " '<binary data>',"
else:
select_sql += " CASE WHEN %s::text='' THEN '''''' ELSE %s::text END," % (column_name, column_name)
select_sql = select_sql[:-1] + " FROM my_table" #remove trailing comma and add table name
c.execute(select_sql)
将空字符串转换为
''
None
价值观。我的代码实际上更广泛,因为它需要创建treeview列等等。希望这能帮助将来来这里的人。