【发布时间】:2016-11-18 06:38:56
【问题描述】:
我正在处理泰卢固语文本以分析一些文本标记。
>>> sent = "నా పేరు కరీం ఉంది. నేను భారత ఆహార ప్రేమ.".decode('utf-8')
>>> text = sent
>>> text = nltk.word_tokenize(text)
>>> result = nltk.pos_tag(text)
>>> for val in result:
... print list(val)[0], list(val)[1]
...
నా JJ
పేరు NNP
కరీం NNP
ఉంది NNP
. .
నేను VB
భారత JJ
ఆహార NNP
ప్రేమ NNP
这样我可以在泰卢固语中看到结果。
对于相同的文本,当我尝试这种方式时,它会在结果中给出 unicode 字符串。 如何在泰卢固语中打印令牌?
>>> s = "నా పేరు కరీం ఉంది. నేను భారత ఆహార ప్రేమ.".decode('utf-8')
>>> res = s.split(' ')
>>> res[0]
u'\u0c28\u0c3e'
>>> type(res[0])
<type 'unicode'>
>>> res[0].encode('ascii')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
>>> res[0].encode('utf-8')
'\xe0\xb0\xa8\xe0\xb0\xbe'
编辑:
print res[0] 正确地给出了它。但是当我执行将此代码放入 .py 脚本并运行它时。它给了
ubuntu@DELL-PC:~/Documents/codes$ python test.py
File "test.py", line 1
SyntaxError: Non-ASCII character '\xe0' in file test.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
test.py 包含:
s = "నా పేరు కరీం ఉంది. నేను భారత ఆహార ప్రేమ.".decode('utf-8')
a = s.split()
for i in a:
print i
【问题讨论】:
-
改用
print res[0]。 -
你有什么问题?为什么不直接
print()结果:print(res[0])? -
对不起我的错误。 print 正确地给出了它。但问题仍然存在。如果您能看到有问题的编辑,我将不胜感激
-
Got to love python - 错误消息甚至包括指向如何处理错误的网页链接。
-
“我的名字是卡里姆。我喜欢印度菜”的有趣文字(字对字)翻译。我正在逆向翻译它,“我叫卡里姆。我爱印度美食”只是为了笑
标签: python python-2.7 unicode utf