为什么从hanzi.to_pinyin()返回一个空字符串?

时间:2015-12-30 17:29:40

标签: python unicode utf-8 ipython

http://dragonmapper.readthedocs.org/en/latest/readme.html

我试图使用dragonmapper将字符转换为拼音,我正在尝试教程,但我被卡住了。 http://dragonmapper.readthedocs.org/en/latest/tutorial.html

def extract(edges):
    dic = {}
    for i in range(len(edges)):
        if edges[i] in dic.keys():
            dic[edges[i]].append(i+1)
        else:
            dic[edges[i]] = [i+1]

    res = []
    for k in sorted(dic.keys()):
        res += combinations(dic[k])

    return res

def combinations(positions):
    ret = []
    print positions
    for i in range(len(positions)):
        for j in range(i+1, len(positions)):
            ret.append(["e"+str(positions[i]), "e"+str(positions[j])])
    print ret
    return ret

R = [1,3,1,6,9,6,1,2,3,0]
res = extract(R)
print res

此时from dragonmapper import hanzi s = '这个字怎么念?' pinyin = hanzi.to_pinyin(s) 是一个空字符串pinyin。我做错了什么?

(也在这里问:https://github.com/tsroten/dragonmapper/issues/11

1 个答案:

答案 0 :(得分:0)

啊哈,我忘了将字符串标记为unicode:

s = u'这个字怎么念?'

或者,使字符串为unicode:

from __future__ import unicode_literals