Python 如何获得格式化输出

Python 如何获得格式化输出,python,python-2.7,pyodbc,Python,Python 2.7,Pyodbc,嗨,我正在使用python和odbc数据库,即Microsoft access。我得到的结果是 (u'EXPANDx TB', 12.0, 10.0, 11.0, 13.0, 0.0, 46.0) (u'EXPANDx TB & GFATM', 1.0, 1.0, 0.0, 1.0, 0.0, 3.0) (u'EXPANDx TB & NRHM', 0.0, 0.0, 1.0, 0.0, 0.0, 1.0) (u'GFATM', 1.0, 1.0, 0.0, 0.0, 0.0,

嗨,我正在使用python和odbc数据库,即Microsoft access。我得到的结果是

(u'EXPANDx TB', 12.0, 10.0, 11.0, 13.0, 0.0, 46.0)
(u'EXPANDx TB & GFATM', 1.0, 1.0, 0.0, 1.0, 0.0, 3.0)
(u'EXPANDx TB & NRHM', 0.0, 0.0, 1.0, 0.0, 0.0, 1.0)
(u'GFATM', 1.0, 1.0, 0.0, 0.0, 0.0, 2.0)
(u'WHO', 3.0, 7.0, 3.0, 5.0, 0.0, 18.0)
(u'GFATM & EXPANDx TB', 1.0, 0.0, 0.0, 0.0, 0.0, 1.0)
额外的术语u正在显示。我也使用了join函数,但得到的错误如下

print ",".join(table1)
TypeError: sequence item 1: expected string or Unicode, float found
 please help me out with this. Thanks in advance. 


 #!/usr/bin/python
 import pyodbc

 db_file = r'''C:\Users\Basavaraj\Documents\C&DST.accdb'''
 user = ''
 password = ''
 odbc_conn_str = 'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s;UID=%s;PWD=%s' % \
            (db_file, user, password)
 conn= pyodbc.connect(odbc_conn_str)
 #for row in cursor.fetchall():
 #   print row[2]
try:
    cursor = conn.cursor()
    cursor.execute("select * from Table1")
    for table1 in cursor.fetchall():
    print ",".join(table1)

finally:
    conn.close()
`只需将每个项目转换为字符串即可

如果要保持unicode项目的原样,则:

print ",".join(str(i) if not isinstance(i, unicode) else i for i in table1)

u'foo'表示这是一个unicode对象。但是你能给出一些代码吗?查询结果可能是一个iterable对象,您可以使用for循环。请尝试打印“,”。joinmapstr,Table感谢falsetru帮助…您不需要内部列表。
print ",".join(str(i) if not isinstance(i, unicode) else i for i in table1)