Python SQLAlchemy的Unicode问题
我知道我在从Unicode转换时遇到了问题,但我不确定它发生在哪里 我正在从一个HTML文件目录中提取最近一次欧洲之旅的数据。某些位置名称具有非ASCII字符(如é、ô、ü)。我使用正则表达式从文件的字符串表示中获取数据 如果我按找到的位置打印位置,它们将使用字符打印,因此编码必须正确:Python SQLAlchemy的Unicode问题,python,unicode,encoding,character-encoding,sqlalchemy,Python,Unicode,Encoding,Character Encoding,Sqlalchemy,我知道我在从Unicode转换时遇到了问题,但我不确定它发生在哪里 我正在从一个HTML文件目录中提取最近一次欧洲之旅的数据。某些位置名称具有非ASCII字符(如é、ô、ü)。我使用正则表达式从文件的字符串表示中获取数据 如果我按找到的位置打印位置,它们将使用字符打印,因此编码必须正确: Le Pré-Saint-Gervais, France Hôtel-de-Ville, France 我使用SQLAlchemy将数据存储在SQLite表中: Base = declarative_base
Le Pré-Saint-Gervais, France
Hôtel-de-Ville, France
我使用SQLAlchemy将数据存储在SQLite表中:
Base = declarative_base()
class Point(Base):
__tablename__ = 'points'
id = Column(Integer, primary_key=True)
pdate = Column(Date)
ptime = Column(Time)
location = Column(Unicode(32))
weather = Column(String(16))
high = Column(Float)
low = Column(Float)
lat = Column(String(16))
lon = Column(String(16))
image = Column(String(64))
caption = Column(String(64))
def __init__(self, filename, pdate, ptime, location, weather, high, low, lat, lon, image, caption):
self.filename = filename
self.pdate = pdate
self.ptime = ptime
self.location = location
self.weather = weather
self.high = high
self.low = low
self.lat = lat
self.lon = lon
self.image = image
self.caption = caption
def __repr__(self):
return "<Point('%s','%s','%s')>" % (self.filename, self.pdate, self.ptime)
engine = create_engine('sqlite:///:memory:', echo=False)
Base.metadata.create_all(engine)
Session = sessionmaker(bind = engine)
session = Session()
当我试图对桌子做任何事情时,比如:
session.query(Point).all()
我得到:
Traceback (most recent call last):
File "./extract_trips.py", line 131, in <module>
session.query(Point).all()
File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.5.4p2-py2.5.egg/sqlalchemy/orm/query.py", line 1193, in all
return list(self)
File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.5.4p2-py2.5.egg/sqlalchemy/orm/query.py", line 1341, in instances
fetch = cursor.fetchall()
File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.5.4p2-py2.5.egg/sqlalchemy/engine/base.py", line 1642, in fetchall
self.connection._handle_dbapi_exception(e, None, None, self.cursor, self.context)
File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.5.4p2-py2.5.egg/sqlalchemy/engine/base.py", line 931, in _handle_dbapi_exception
raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect)
sqlalchemy.exc.OperationalError: (OperationalError) Could not decode to UTF-8 column 'points_location' with text 'Le Pré-Saint-Gervais, France' None None
回溯(最近一次呼叫最后一次):
文件“/extract_trips.py”,第131行,在
session.query(Point.all)()
文件“/usr/lib/python2.5/site packages/SQLAlchemy-0.5.4p2-py2.5.egg/SQLAlchemy/orm/query.py”,第1193行,共
返回列表(自我)
文件“/usr/lib/python2.5/site packages/SQLAlchemy-0.5.4p2-py2.5.egg/SQLAlchemy/orm/query.py”,第1341行,在实例中
fetch=cursor.fetchall()
fetchall中的文件“/usr/lib/python2.5/site packages/SQLAlchemy-0.5.4p2-py2.5.egg/SQLAlchemy/engine/base.py”,第1642行
self.connection.\u handle\u dbapi\u异常(e,None,None,self.cursor,self.context)
文件“/usr/lib/python2.5/site packages/SQLAlchemy-0.5.4p2-py2.5.egg/SQLAlchemy/engine/base.py”,第931行,在_handle_dbapi_exception中
raise exc.DBAPIError.instance(语句、参数、e、连接无效=断开连接)
sqlalchemy.exc.OperationalError:(OperationalError)无法解码为UTF-8列“points_location”,文本为“Le Pré-Saint Gervais,France”无
我希望能够正确地存储,然后返回原始字符完整的位置名称。任何帮助都将不胜感激。尝试使用Unicode列类型而不是字符串作为Unicode列:
Base = declarative_base()
class Point(Base):
__tablename__ = 'points'
id = Column(Integer, primary_key=True)
pdate = Column(Date)
ptime = Column(Time)
location = Column(Unicode(32))
weather = Column(String(16))
high = Column(Float)
low = Column(Float)
lat = Column(String(16))
lon = Column(String(16))
image = Column(String(64))
caption = Column(String(64))
编辑:对评论的回应:
如果您收到有关unicode编码的警告,则可以尝试以下两种方法:
False
或None
打开
在Unicode类型上创建_engine()和字符串,'warn'
。什么时候
True
,
导致所有unicode转换操作引发
当
非unicode bytestring作为绑定参数传递。”“警告”结果
在警告中。强烈建议所有unicode用户
应用
正确使用Python unicode对象(即u'hello'和not
‘你好’)
因此,数据往返准确
我认为您正在尝试输入一个非unicode bytestring。也许这会引导你走上正确的道路?需要某种形式的转换,比较“hello”和u“hello”
干杯我发现这篇文章在某种程度上解释了我的问题: 通过使用“编解码器”模块,然后按如下方式更改程序,我能够获得所需的结果: 打开文件时:
infile = codecs.open(filename, 'r', encoding='iso-8859-1')
打印位置时:
print location.encode('ISO-8859-1')
我现在可以查询和操作表中的数据,而不会出现以前的错误。我只需要在输出文本时指定编码
(我仍然不完全理解这是如何工作的,所以我想是时候学习更多关于Python的unicode处理了…谢谢你的建议。我认为这是我正确的方向。我现在收到有关我插入的数据编码的警告,但我不确定如何修复此问题。我已经更新了我的问题以反映您的建议。我会在“iso-8859-1”之前先尝试“cp1252”。我不知道下面这些是否有帮助:
infile = codecs.open(filename, 'r', encoding='iso-8859-1')
print location.encode('ISO-8859-1')