背景
之前寫過一個遍歷文件夾進行處理的Python程序,但因為時間太久找不着了。。 導致只能自己再寫一遍,於是決定將代碼放置於博客之中,以便以后使用。
#!usr/bin/env python
#-*- coding:utf-8 -*-
import math
import os
import glob
import numpy as np
import jieba
import string
import jieba.analyse
def read_from_file(directions):
decode_set=['utf-8','gb18030','ISO-8859-2','gb2312','gbk','Error']#編碼集
#編碼集循環
for k in decode_set:
try:
file = open(directions,"r",encoding=k)
readfile = file.read()#這步如果解碼失敗就會引起錯誤,跳到except。
#print("open file %s with encoding %s" %(directions,k))#打印讀取成功
#readfile = readfile.encode(encoding="utf-8",errors="replace")#若是混合編碼則將不可編碼的字符替換為"?"。
file.close()
break#打開路徑成功跳出編碼匹配
except:
if k=="Error":#如果碰到這個程序終止運行
raise Exception("%s had no way to decode"%directions)
continue
return readfile
filenames = []
filenames=glob.glob(r"C:/Users/Administrator/Documents/Tencent Files/9/FileRecv/TXT/*.txt")
filenameslen=len(filenames)
count=0
for filename in filenames:
print("%d : %d" %(count,filenameslen))
names=filename.find('TXT')+4
namee=filename.find('.txt')
f=open(filename,"rb")
content=f.readlines()
content=" ".join('%s' %id for id in content)
start=content.find('description')+16
overflow=content.find('comments')
end=content[start:].find('#')+start
if end>=overflow:
end=overflow
file = open(r"C:/Users/Administrator/Documents/Tencent Files/9/FileRecv/TXT/" +filename[names:namee]+'keyword' + '.txt','w')
file_data = content[start:end]
#基於TF-IDF算法進行關鍵詞抽取
tfidf=jieba.analyse.extract_tags
keywords=tfidf(file_data)
for i in range(len(keywords)):
if len(keywords)<=0:
print("error,please check your input")
break
file.write(keywords[i]+'\n')
file.close()
count=count+1
print("%d : %d" %(count,filenameslen))
print("finished")
