简体   繁体   English

pyodbc + MS Access(*。mdb)+ UnicodeDecodeError

[英]pyodbc + MS Access(*.mdb) + UnicodeDecodeError

I connect to MS Access database (mdb file) via pyodbc. 我通过pyodbc连接到MS Access数据库(mdb文件)。

Some data in this db have polish char like (łóźćśę and so on). 此数据库中的某些数据具有波兰字符,例如(łóźćśę等)。 When I fetch some data, polish chars are replaced by strange chars (³, ê). 当我获取一些数据时,波兰字符被奇怪的字符(³,ê)取代。 I try to decode into utf8, cp1250, cp1252, latin1, latin2 but it does't solve my problem (still char are not correct). 我尝试将其解码为utf8,cp1250,cp1252,latin1,latin2,但它不能解决我的问题(仍然是char不正确)。

Can anyone helps me? 谁能帮我?

ps. PS。 for now my solution is data = data.replace('\\xc2\\xb3', 'ł') but it is ugly as hell. 现在,我的解决方案是data = data.replace('\\xc2\\xb3', 'ł')但这很丑陋。

I have an .mdb file with some sample data in a table named [vocabulary]. 我在一个名为[vocabulary]的表中有一个.mdb文件,其中包含一些示例数据。 When I launch Access and open the table in Datasheet View it looks like this: 当我启动Access并在数据表视图中打开表时,它看起来像这样:

ID  word      language  english_equiv
--  --------  --------  -------------
 5  żaglówka  Polish    sailboat

The following Python 2.7.5 code 以下Python 2.7.5代码

# -*- coding: utf-8 -*-
import pyodbc

db = pyodbc.connect(
    r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};' +
    r'DBQ=C:\__tmp\unicodeMdbTest.mdb')

cursor1 = db.execute('SELECT [word] FROM [vocabulary] WHERE [ID]=5')

while 1:
    row = cursor1.fetchone()
    if not row:
        break
    print row.word
db.close()

successfully prints the following in the IDLE shell 在IDLE shell中成功打印以下内容

żaglówka

Note the file encoding declaration on the first line of the .py file. 请注意.py文件第一行上的文件编码声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM