【问题标题】:UnicodeError with SqlAlchemy + Firebird + FDBSqlAlchemy + Firebird + FDB 的 UnicodeError
【发布时间】:2018-12-28 04:48:34
【问题描述】:

我正在尝试显示来自 firebird 3.x 数据库的结果,但得到:

文件 "/...../Envs/pos/lib/python3.6/site-packages/fdb/fbcore.py", 第 479 行,在 b2u return st.decode(charset) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd1 in position 9: invalid continuation byte

尽管我到处都设置了 utf-8:

# -- coding: UTF-8 -- 

import os

os.environ["PYTHONIOENCODING"] = "utf8"

from sqlalchemy import *

SERVIDOR = "localhost"
BASEDATOS_1 = "db.fdb"

PARAMS = dict(
    user="SYSDBA",
    pwd="masterkey",
    host="localhost",
    port=3050,
    path=BASEDATOS_1,
    charset='utf-8'
)

firebird = create_engine("firebird+fdb://%(user)s:%(pwd)s@%(host)s:%(port)d/%(path)s?charset=%(charset)s" % PARAMS, encoding=PARAMS['charset'])

def select(eng, sql):
    with eng.connect() as con:
        return eng.execute(sql)

for row in select(firebird, "SELECT * from clientes"):
    print(row)

【问题讨论】:

  • 您找到解决方案了吗?
  • 您找到解决方案了吗? Me ajuda pelo amor de deus, to nisso a um tempo ja tambem

标签: python python-3.x unicode sqlalchemy


【解决方案1】:

我遇到了同样的问题。
在我的情况下,数据库不是 UTF-8。 在连接字符串中设置正确的字符集后,它起作用了:?charset=ISO8859_1

【讨论】:

    【解决方案2】:

    我会尝试使用模块unidecode

    您的脚本在尝试转换时会崩溃,因此此模块可以帮助您。正如他们在模块文档中所说:

    模块导出一个接受 Unicode 对象的函数 (Python 2.x) 或字符串 (Python 3.x) 并返回一个字符串(可以是 在 Python 3.x 中编码为 ASCII 字节)

    首先你使用 pip 下载它,然后试试这个:

    import unidecode
    
    ...
    if type(line) is unicode:
       line = unidecode.unidecode(line)
    

    希望它能解决你的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      • 2013-11-13
      • 1970-01-01
      • 2020-03-07
      • 2013-07-16
      相关资源
      最近更新 更多