如何计算从文件中读取的字符串中的单词?

我正在尝试创建一个程序,它采用给定路径中的所有文本文件并将所有字符串保存在一个列表中:


import os

import collections


vocab = set()

path = 'a\\path\\'


listing = os.listdir(path)

unwanted_chars = ".,-_/()*"

vocab={}

for file in listing:

    #print('Current file : ', file)

    pos_review = open(path+file, "r", encoding ='utf8')

    words = pos_review.read().split()

    #print(type(words))

    vocab.update(words)

pos_review.close()


print(vocab)

pos_dict = dict.fromkeys(vocab,0)

print(pos_dict)

输入


file1.txt: A quick brown fox.

file2.txt: a quick boy ran.

file3.txt: fox ran away.

输出


A : 2

quick : 2

brown : 1

fox : 2

boy : 1

ran : 2

away : 1

到目前为止,我能够制作这些字符串的字典。但现在不知道如何在所有文本文件中组合键、值对字符串及其频率。


Smart猫小萌
浏览 212回答 3
3回答

小唯快跑啊

希望这可以帮助,import osimport collectionsvocab = set()path = 'a\\path\\'listing = os.listdir(path)unwanted_chars = ".,-_/()*"vocab={}whole=[]for file in listing:    #print('Current file : ', file)    pos_review = open(path+file, "r", encoding ='utf8')    words = pos_review.read().split()    whole.extend(words)pos_review.close()print(vocab)d={} #Creating an Empty dictionaryfor item in whole:    if item in d.keys():        d[item]+=1 #Update count    else:        d[item]=1print(d)

幕布斯7119047

这也有效import pandas as pdimport glob.globfiles = glob.glob('test*.txt')txts = []for f in files:    with open (f,'r') as t: txt = t.read()    txts.append(txt)texts=' '.join(txts)df = pd.DataFrame({'words':texts.split()})out = df.words.value_counts().to_dict()

蛊毒传说

使用collections.Counter:Counter是dict计算可迭代的子类数据给定 3 个文件,命名为t1.txt, t2.txt&t3.txt每个文件包含以下 3 行文本file1 txt A quick brown fox.file2 txt a quick boy ran.file3 txt fox ran away.代码:获取文件:路径库from pathlib import Pathfiles = list(Path('e:/PythonProjects/stack_overflow/t-files').glob('t*.txt'))print(files)# Output[WindowsPath('e:/PythonProjects/stack_overflow/t-files/t1.txt'), WindowsPath('e:/PythonProjects/stack_overflow/t-files/t2.txt'), WindowsPath('e:/PythonProjects/stack_overflow/t-files/t3.txt')]收集并计算单词:创建一个单独的函数 ,clean_str用于清理每一行文本str.lower对于小写字母str.translate, str.maketrans&string.punctuation用于高度优化的标点删除从最好的方式从字符串中去除标点符号from collections import Counterimport stringdef clean_string(value: str) -> list:    value = value.lower()    value = value.translate(str.maketrans('', '', string.punctuation))    value = value.split()    return valuewords = Counter()for file in files:    with file.open('r') as f:        lines = f.readlines()        for line in lines:            line = clean_string(line)            words.update(line)print(words)# OutputCounter({'file1': 3,         'txt': 9,         'a': 6,         'quick': 6,         'brown': 3,         'fox': 6,         'file2': 3,         'boy': 3,         'ran': 6,         'file3': 3,         'away': 3})名单words:list_words = list(words.keys())print(list_words)>>> ['file1', 'txt', 'a', 'quick', 'brown', 'fox', 'file2', 'boy', 'ran', 'file3', 'away']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python