我正在用python进行z39.50搜索,但是在解码搜索结果时遇到问题。在
《哈利波特》的第一个搜索结果显然是这本书的希伯来语版本。在
如何将其转换为unicode?在
这是我用来获取帖子的最小代码:
#!/usr/bin/env python
# encoding: utf-8
from PyZ3950 import zoom
from PyZ3950 import zmarc
conn = zoom.Connection('z3950.loc.gov', 7090)
conn.databaseName = 'VOYAGER'
query = zoom.Query('CCL', 'ti="HARRY POTTER"')
res = conn.search(query)
print "%d hits:" % len(res)
for r in res[:1]:
print unicode( r.data )
运行脚本会导致“UnicodeDecodeError:'ascii'codec无法解码位置788中的字节0xf2:序号不在范围(128)”
我试图重现您的问题,但我正在进入Python中相当于“DLL地狱”的部分。请指定您正在使用的每个(Python、PyZ3950和PLY)版本。在
您将从错误消息中注意到,在获得非ASCII字节之前,有788个ASCII字节。听起来不像希伯来语/阿拉伯语/希腊语/西里尔文/等等,它们使用非ASCII字节来表示这些语言中最常用的字符。在
不是
print unicode(r.data)
,而是print type(r.data), repr(r.data)
并编辑问题以显示结果。在Update我设法让它在最新版本的PyZ3950上运行,并使用python2.6进行PLY,而不是PyZ3950中的
import lex
/ccl.py公司(同样地,修复了import yacc
。在以下是转储hit 0和hit 200的结果:
您会注意到,在“ASCII”部分爆炸之前,有很多\x1e和\x1f。在每个转储的末尾还有一个\x1d。(GROUP | UNIT | RECORD)可能是分隔符。您还将注意到第二个输出看起来也像gobbledegook,但它没有提到希伯来语。在
结论:忘了希伯来语。忘记Unicode吧,东西不是
sensible_unicode_text.encode("any_known_encoding")
的结果。Z3950充满了穿孔卡片、磁鼓和磁带的气味。如果它知道Unicode,那么在数据中就不明显了。在看起来您需要阅读PyZ3950附带的zoomapi文档,这将引导您找到ZOOM docs。。。祝你好运。在
更新2
^{pr2}$看来你需要了解MARC
更新3在第一个转储中注意到了BIDI的东西,比如
‏‪[2008]‬
。。。所以最终你会得到Unicode,在你深入到各个层次的文档中,找出什么是包装在什么里面的。。。再次祝你好运!在您需要为此转换Marc数据: 您可以使用以下代码:
你必须找出他们使用的正确编码,并将其替换为“windows-1255”(如果你对希伯来语的猜测是正确的话,这可能有用)。在
相关问题 更多 >
编程相关推荐