Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy的Unicode问题_Python_Unicode_Encoding_Character Encoding_Sqlalchemy - Fatal编程技术网

Python SQLAlchemy的Unicode问题

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

我知道我在从Unicode转换时遇到了问题,但我不确定它发生在哪里

我正在从一个HTML文件目录中提取最近一次欧洲之旅的数据。某些位置名称具有非ASCII字符(如é、ô、ü)。我使用正则表达式从文件的字符串表示中获取数据

如果我按找到的位置打印位置,它们将使用字符打印,因此编码必须正确:

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编码的警告,则可以尝试以下两种方法:

  • 将您的位置转换为unicode。这意味着你的观点是这样创建的:

    newpoint=Point(文件名、更新日期、ptime、unicode(位置)、天气、高、低、纬度、长、图像、标题)

    当传递字符串或unicode字符串时,unicode转换将生成unicode字符串,因此您无需担心传递的内容

  • 如果这不能解决编码问题,请尝试在unicode对象上调用encode。这意味着使用如下代码:

    newpoint=Point(文件名、更新日期、ptime、unicode(位置)。编码('utf-8')、天气、高低、纬度、经度、图像、标题)

    这一步可能没有必要,但它实际上是将unicode对象从unicode代码点转换为特定的字节表示形式(在本例中为utf-8)。当您传入unicode对象时,我希望SQLAlchemy能为您做到这一点,但它可能不会

  • 见第0.4.2节

    向字符串和 创建引擎(), 断言_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')