0

我正在使用 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 中执行此操作时会发生此错误。

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

4

2 回答 2

2

你有一个旧版本,至少在 2014 年 10 月 18 日之前。这个错误应该在最新版本中修复。

提交错误修复的 SHA

于 2014-11-06T21:06:54.540 回答
0

尝试# -*- coding: utf-8 -*-在代码顶部添加(通常在 shebang 语句下方),如下所示:

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

我自己遇到了 Unicode 和 Python2 的一些问题。我将我的代码重写为 Python3,它可以“开箱即用”处理 Unicode 文本

于 2014-11-05T03:20:14.487 回答