使用pyorient时长查询引发Python UnicodeDecodeError

0 投票
2 回答
553 浏览
提问于 2025-05-01 17:16

我正在使用OrientDB和pyorient通过Python来查询数据库。我的问题出现在尝试执行一个比较长的查询时:

select * from (traverse in('written_on') from (select * from (traverse in('posted_by') from (select * from person where @rid=#14:0) ) ) where @class='Comment'

出现了一个UcodingDecodingError错误。以下是我的代码:

$ python 
Python 2.7.3 (default, Feb 27 2014, 19:58:35) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyorient 
>>> client = pyorient.OrientDB('localhost',2424)
>>> client.connect('root','root')
10
>>> db = client.db_open('DummyData','root','root')
>>> persons=client.query('select * from person')
>>> for person in persons:
...     print person.name
... 
[GOOD RESULT]
>>> posts=client.query('select * from (traverse in('posted_by') from (select * from person where @rid=#14:0) ) where @class='Post')
  File "<stdin>", line 1
    posts=client.query('select * from (traverse in('posted_by') from (select * from person where @rid=#14:0) ) where @class='Post')
                                                            ^
SyntaxError: invalid syntax
>>> posts=client.query("select * from (traverse in('posted_by') from (select * from person where @rid=#14:0) ) where @class='Post'")
>>> for post in posts:
...     print post.content
... 
[ANOTHER GOOD RESULT]
>>> comments=client.query("select * from (traverse in('written_on') from (select * from (traverse in('posted_by') from (select * from person where @rid=#14:0) ) ) where @class='Comment'")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/pyorient/orient.py", line 212, in query
    .prepare(( QUERY_SYNC, ) + args).send().fetch_response()
  File "/usr/local/lib/python2.7/dist-packages/pyorient/utils.py", line 47, in wrap_function
    return wrap(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pyorient/utils.py", line 60, in wrap_function
    return wrap(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pyorient/messages/commands.py", line 131, in prepare
    return super( CommandMessage, self ).prepare()
  File "/usr/local/lib/python2.7/dist-packages/pyorient/messages/base.py", line 72, in prepare
    self._encode_field( x ) for x in self._fields_definition
  File "/usr/local/lib/python2.7/dist-packages/pyorient/messages/base.py", line 72, in <genexpr>
    self._encode_field( x ) for x in self._fields_definition
  File "/usr/local/lib/python2.7/dist-packages/pyorient/messages/base.py", line 200, in _encode_field
    buf = v.encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0x9e in position 65: ordinal not in range(128)
>>> 

虽然最后这个查询在OrientDB Studio上可以正常运行并且能得到正确的结果,但在Python中执行时却出现了这个错误。

注意:数据库中存储的数据是用阿拉伯语写的。

暂无标签

2 个回答

0

试着在你的代码最上面加上 # -*- coding: utf-8 -*- 这一行(通常放在开头的那行代码下面),像这样:

#!/usr/bin/python
# -*- coding: utf-8 -*-

我自己在使用Python2的时候也遇到了一些关于Unicode的问题。后来我把代码改成了Python3,因为Python3对Unicode文本的处理非常简单,不需要额外的设置。

2

你使用的是一个旧版本,至少是在2014年10月18日之前的版本。这个问题在最新的版本中应该已经修复了。

这是修复这个问题的提交记录链接

撰写回答