在python上使用希伯来语 [英] Using hebrew on python
问题描述
计数我给定文本中的单词数量(这是希伯来语)。该计数器确实计算
的单词,并识别语言,因为我使用
# - * - 编码:utf-8 - * -
p> 问题是,当我打印我的计数器,我会得到奇怪的符号。 (我使用eclipse)
这是代码和打印:
# - * - 编码:utf- 8 - * -
import string
from collections import Counter
class classifier:
def __init __(self,filename):
self.myFile = open(filename)
self.cnt = Counter()
def generateList(self):
exclude = set(string.punctuation)
for self.myFile中的行:
for words in lines.split():
如果word不在排除中:
nWord =
为单词中的字母:
如果字母在排除中:
letter =
nWord + = letter
else:
nWord + = letter
self.cnt [nWord] + = 1
print self.cnt
打印:
Counter({'\xd7\x97\xd7\x94':465,'\xd7\x96\xd7\ x95':432,'\xd7\xa1\xd7\x92\xd7\x95\xd7\xa8':421,'\xd7\x94\xd7\x92\xd7 \x91':413})
有关如何以正确的方式打印单词的任何想法?
你得到的奇怪的符号是python表示unicode字符串的方式。
您需要对它们进行解码,例如:
>>>打印'\xd7\x97 \xd7\x94'.decode('UTF8')
חה
I have a problem printing hebrew words. i am using the counter module in order to
count number of words in my given text (which is in hebrew). the counter indeed counts
the words, and identifies the language because i am using # -*- coding: utf-8 -*-
The problem is, when i print my counter, i get weird symbols. (I am using eclipse) Here is the code and the printings:
# -*- coding: utf-8 -*-
import string
from collections import Counter
class classifier:
def __init__(self,filename):
self.myFile = open(filename)
self.cnt = Counter()
def generateList(self):
exclude = set(string.punctuation)
for lines in self.myFile:
for word in lines.split():
if word not in exclude:
nWord = ""
for letter in word:
if letter in exclude:
letter = ""
nWord += letter
else:
nWord += letter
self.cnt[nWord]+=1
print self.cnt
Printings:
Counter({'\xd7\x97\xd7\x94': 465, '\xd7\x96\xd7\x95': 432, '\xd7\xa1\xd7\x92\xd7\x95\xd7\xa8': 421, '\xd7\x94\xd7\x92\xd7\x91': 413})
Any idea on how to print the words in the right way?
The "weird symbols" you are getting is python's way of representing unicode strings.
You need to decode them, for example:
>>>print '\xd7\x97\xd7\x94'.decode('UTF8')
חה
这篇关于在python上使用希伯来语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!