python IDLE以及控制台中无法正确显示中文
题目是中文分词,读取了一个字典文件,输出分词结果;在IDLE中无法显示,按慕课尚老师说的在控制台运行也不行求大佬指点QAQ代码如下:#-*-coding:UTF-8-*-...
题目是中文分词,读取了一个字典文件,输出分词结果;
在IDLE中无法显示,按慕课尚老师说的在控制台运行也不行
求大佬指点QAQ
代码如下:
# -*- coding:UTF-8 -*-
import sys
type = sys.getfilesystemencoding()
def load_dic():
f=open('C:/Python27/Lib/text/lexicon.dic','r')
word_dic=set()
max_length=1
for line in f:
word=unicode(line.strip(),'utf-8')
word_dic.add(word)
if len(word)>max_length:
max_length=len(word)
return max_length,word_dic
def fmm_word_seg(sentence,word_dic,max_length):
begin=0
words=[]
while begin < len(sentence):
for end in range(min(begin+max_length,sentence),begin,-1):
word=sentence[begin:end]
if word in word_dic or end == begin+1:
words.append(word)
break
begin=end
return words
max_length,word_dic=load_dic()
words=fmm_word_seg(raw_input('input'),word_dic,max_length)
for word in words:
print word 展开
在IDLE中无法显示,按慕课尚老师说的在控制台运行也不行
求大佬指点QAQ
代码如下:
# -*- coding:UTF-8 -*-
import sys
type = sys.getfilesystemencoding()
def load_dic():
f=open('C:/Python27/Lib/text/lexicon.dic','r')
word_dic=set()
max_length=1
for line in f:
word=unicode(line.strip(),'utf-8')
word_dic.add(word)
if len(word)>max_length:
max_length=len(word)
return max_length,word_dic
def fmm_word_seg(sentence,word_dic,max_length):
begin=0
words=[]
while begin < len(sentence):
for end in range(min(begin+max_length,sentence),begin,-1):
word=sentence[begin:end]
if word in word_dic or end == begin+1:
words.append(word)
break
begin=end
return words
max_length,word_dic=load_dic()
words=fmm_word_seg(raw_input('input'),word_dic,max_length)
for word in words:
print word 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询