我無法得到任何東西寫在我的outut文件(word_count.txt)。無法獲得獨特的字/詞計數器的工作 - Python
我希望腳本在我的phrases.txt文檔中查看所有500個短語,並輸出所有單詞的列表以及它們出現的次數。
from re import findall,sub
from os import listdir
from collections import Counter
# path to folder containg all the files
str_dir_folder = '../data'
# name and location of output file
str_output_file = '../data/word_count.txt'
# the list where all the words will be placed
list_file_data = '../data/phrases.txt'
# loop through all the files in the directory
for str_each_file in listdir(str_dir_folder):
if str_each_file.endswith('data'):
# open file and read
with open(str_dir_folder+str_each_file,'r') as file_r_data:
str_file_data = file_r_data.read()
# add data to list
list_file_data.append(str_file_data)
# clean all the data so that we don't have all the nasty bits in it
str_full_data = ' '.join(list_file_data)
str_clean1 = sub('t','',str_full_data)
str_clean_data = sub('n',' ',str_clean1)
# find all the words and put them into a list
list_all_words = findall('w+',str_clean_data)
# dictionary with all the times a word has been used
dict_word_count = Counter(list_all_words)
# put data in a list, ready for output file
list_output_data = []
for str_each_item in dict_word_count:
str_word = str_each_item
int_freq = dict_word_count[str_each_item]
str_out_line = '"%s",%d' % (str_word,int_freq)
# populates output list
list_output_data.append(str_out_line)
# create output file, write data, close it
file_w_output = open(str_output_file,'w')
file_w_output.write('n'.join(list_output_data))
file_w_output.close()
任何幫助將是巨大的(尤其是如果我能輸出列表中的實際輸出「單」字。
非常感謝。
您在粘貼的代碼中存在縮進問題。從'with'語句縮進行以將它們放入循環中。 – kaveh
嘿西蒙,它看起來像你可能是新來的SO。如果您覺得答案可以解決問題,請點擊綠色複選標記將其標記爲「已接受」。這有助於將注意力集中在仍然沒有答案的舊版SO上。 –
謝謝@robertrodkey全部完成。週末愉快。 –