我目前正在使用Python 2.7,包含Tkinter和集合。我有我的程序来计算.txt
文件中的单词并将其保存到变量中。但是当我resultsBox.insert(END, document_data_Tally )
时,它显示为Counter({'Test': 2, 'World': 1, 'Hello': 1})
。
我想把它变成
的格式Word : Value Word : Value
Word : Value Word : Value
Word : Value Word : Value
我想让它为10行做,然后从下一列开始。
import os
from Tkinter import *
from collections import Counter
import tkFileDialog
root = Tk()
root.title("Doc Word Frequency")
root.geometry("600x300")
def close_window ():
root.destroy()
def browse_directory():
global filename
filename = tkFileDialog.askopenfilename(parent=root,title='Choose a file')
inputBox.delete(0, "end")
inputBox.insert(0, filename)
def read_document():
with open (os.path.basename(filename), "r") as myfile:
document_data = myfile.read().replace('\n', '')
document_data_list = re.sub("[^\w]", " ", document_data).split()
document_data_Tally = Counter(document_data_list)
print(document_data_list)
resultsBox.configure(state=NORMAL)
resultsBox.insert(END, document_data_Tally )
resultsBox.configure(state=DISABLED)
inputBox = Entry(root, width = 50)
inputBox.grid(row = 0, column = 0, padx = 20, pady = 20)
inputBox.insert(0, 'Upload Document')
Button(root, width = 9, text = 'Browse', command = browse_directory).grid(row = 0, column = 1, sticky = W, padx = 4)
Button(root, width = 9, text = 'Read', command = read_document).grid(row = 0, column = 2, sticky = W, padx = 4)
Button(root, width = 9, text = 'Quit', command = close_window).grid(row = 0, column = 3, sticky = W, padx = 4)
resultsBox = Text(root, width = 60, height = 10)
resultsBox.grid(columnspan = 4, padx = 10, pady = 10)
resultsBox.config(state=DISABLED)
mainloop( )
PS。我对Python很陌生,任何建设性的批评都会受到赞赏。
答案 0 :(得分:2)
在将数据放入文本之前,您需要从计数器对象中提取数据:
data = '\n'.join('{}\t{}'.format(k, v) for k, v in document_data_Tally.iteritems())
resultsBox.configure(state=NORMAL)
resultsBox.insert(END, data)
resultsBox.configure(state=DISABLED)
这会构建一个列表,用单独的标签分隔单词及其计数,因此您应该在resultsBox
中看到与下面类似的内容:
Test 2
World 1
Hello 1