结巴分词python

将文件中的txt文档依次读出 并分好词后 写入 另外的TXT中

#coding=utf-8
import os
import jieba
import codecs
import random
def readFile(newDir):
f=open(newDir,"r",encoding="utf-8")
string=f.read()
print(string)
seg_list = jieba.cut(string)
s=" ".join(seg_list)
print(s)
m=list(s)
dir="C:/Users/PC/Desktop/分好类/娱乐/"+str(random.randint(0,10000000))+ '.txt'
f = open(dir, 'wb+')
for word in m:
f.write(word.encode('utf-8'))
f.close()
return


def eachFile(filepath):
pathDir = os.listdir(filepath)
for s in pathDir:
newDir=os.path.join(filepath,s)
if os.path.isfile(newDir):
if os.path.splitext(newDir)[1]==".txt":
readFile(newDir)
pass
else:
eachFile(newDir) #如果不是文件,递归这个文件夹的路径
eachFile("C:")

 

猜你喜欢

转载自www.cnblogs.com/tyyhph/p/9038137.html