• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
野野
博客园    首页    新随笔    联系   管理    订阅  订阅
结巴分词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:")

 

 

posted on 2018-05-14 21:04  野野  阅读(136)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3