Question

Hi i'm using the python and odbc database i.e Microsoft access. I'm getting the output like

(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)

the extra term "u" is displaying. I also used the join function but getting the error like

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()

`

Was it helpful?

Solution

Just convert each item to string.

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

If you want to keep the unicode items as is, then :

print ",".join(str(i) if not isinstance(i, unicode) else i for i in table1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top