python - UnicodeDecodeError : 'utf-16-le'

标签 python ms-access odbc pyodbc utf-16le

我正在尝试从 Access 数据库中提取表列表。该脚本通过并显示大约三分之一的表并收到以下错误:

Traceback (most recent call last): File "C:/GageInfo/sourcecode for GageTrakNotify/__test script.py", line 31, in for fld in cursor2.columns(rows.table_name): UnicodeDecodeError: 'utf-16-le' codec can't decode bytes in position 132-133: illegal encoding



任何提示将不胜感激。
import pyodbc


cursor1 = conn.cursor()
cursor2 = conn.cursor()

tblCount = 0
for rows in cursor1.tables():
   if rows.table_type == "TABLE":
       tblCount += 1
       print(rows.table_name)
       for fld in cursor2.columns(rows.table_name):
           print(fld.table_name, fld.column_name)

conn.close()

最佳答案

这几乎肯定是由 cursor.columns() 的已知问题引起的。如果表元数据包括列的“描述”,则在使用 Access ODBC 驱动程序时。以下 GitHub 问题包括对该问题和可能的解决方法的讨论:
https://github.com/mkleehammer/pyodbc/issues/328
TL;DR:在这些情况下,Access ODBC 驱动程序返回尾随垃圾字节,并且某些字节组合将不是有效的 UTF-16LE 编码字符。 pyodbc 输出转换器函数可用于拦截字节字符串并对其进行解码,而不会引发异常。此处描述了具体的解决方法:
https://github.com/mkleehammer/pyodbc/issues/328#issuecomment-419655266

关于python - UnicodeDecodeError : 'utf-16-le' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47645224/

相关文章:

python - 检查二进制表示中是否存在位序列

Python 子进程返回错误的退出代码

ms-access - 如何将 MS Access 数据库编码转换为 UTF-8?

mysql - PowerShell:ODBC DSN 连接中的多个查询

Python:使用父类的构造函数初始化对象的成员?

python - 如何将列表列表写入 txt 文件?

excel - 通过 Access VBA 格式化 Excel - 范围应用程序/对象定义的错误

sql - 是否可以在 Access 的连接条件中使用子查询?

postgresql - Postgresql ODBC 自动提交模式的真正含义是什么

c - 如何在 C 中返回 SQLPOINTER 数组?