# 进入的path是个文件夹路径,里面的文件是txt def eachFile(filepath): pathDir = os.listdir(filepath) # 获取当前路径下的文件名,返回List title = [] key1 = [];key2 = [];key3 = [];key4 = [];key5 = [];key6 = [];key7 = [];key8 = [];key9 = [];key10 = [] key_words = [key1, key2, key3, key4, key5, key6, key7, key8, key9, key10] for s in pathDir: newDir = os.path.join(filepath, s) # 将文件命加入到当前文件路径后面 if os.path.isfile(newDir) and os.path.splitext(newDir)[1] == ".txt": # 如果是txt文件 with open(newDir, 'r') as f: content = f.read() if content: title.append(s.replace('.txt', '')) tags = jieba.analyse.extract_tags(content, topK=10, allowPOS=('n','v')) for i in range(len(key_words)): try: key_words[i].append(tags[i]) except: key_words[i].append('') result = pd.DataFrame({'title': title, 'key1': key1, 'key2': key2, 'key3': key3, 'key4': key4, 'key5': key5, 'key6': key6, 'key7': key7, 'key8': key8, 'key9': key9, 'key10': key10, }, columns=['title', 'key1', 'key2', 'key3', 'key4', 'key5', 'key6', 'key7', 'key8', 'key9', 'key10', ]) return result