如何获取格式化的输出

时间:2013-12-20 06:10:55

标签: python python-2.7 pyodbc

您好我正在使用python和odbc数据库,即Microsoft访问。我得到了像

这样的输出
(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)

额外的术语“你”正在显示。我也使用了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()

`

1 个答案:

答案 0 :(得分:0)

只需将每件商品转换为string

print ",".join(str(i) for i in table1)

如果您想保留unicode项目,那么:

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