1
votes

I am using Python 2.7 to access a database in MS Access with pypyodbc. However, my list is in Unicode instead of utf8

My code is as follows:

listofvariants=list()
conn=pypyodbc.win_connect_mdb("C:\Users\Database.mdb")
cursor = conn.cursor()
cursor.execute("SELECT Name FROM Variant")
for row in cursor.fetchall():
    listofvariants.append(row)
print(listofvariants)
cursor.close()
conn.close()

My results are

["(u'Base Variant                                      ',)", "(u'First           Variant                                  ',)"

Instead of

["Base Variant", "First Variant"]

I have tried to manipulate it with row.encode('UTF-8') but I get the error message: AttributeError: 'Row' object has no attribute 'encode'

1

1 Answers

2
votes

If you are only retrieving the one column then you could simply replace

for row in cursor.fetchall():
    listofvariants.append(row)

with

for row in cursor.fetchall():
    listofvariants.append(row[0].encode('utf_8'))