我是Python的绝对初学者。我正在对希腊戏剧进行文本分析并计算每个单词的词频。因为播放很长,我无法看到我的全部数据,它只显示频率最低的单词,因为Python窗口中没有足够的空间。我正在考虑将其转换为.csv文件。我的完整代码如下:
#read the file as one string and spit the string into a list of separate words
input = open('Aeschylus.txt', 'r')
text = input.read()
wordlist = text.split()
#read file containing stopwords and split the string into a list of separate words
stopwords = open("stopwords .txt", 'r').read().split()
#remove stopwords
wordsFiltered = []
for w in wordlist:
if w not in stopwords:
wordsFiltered.append(w)
#create dictionary by counting no of occurences of each word in list
wordfreq = [wordsFiltered.count(x) for x in wordsFiltered]
#create word-frequency pairs and create a dictionary
dictionary = dict(zip(wordsFiltered,wordfreq))
#sort by decreasing frequency and print
aux = [(dictionary[word], word) for word in dictionary]
aux.sort()
aux.reverse()
for y in aux: print y
import csv
with open('Aeschylus.csv', 'w') as csvfile:
fieldnames = ['dictionary[word]', 'word']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'dictionary[word]': '1', 'word': 'inherited'})
writer.writerow({'dictionary[word]': '1', 'word': 'inheritance'})
writer.writerow({'dictionary[word]': '1', 'word': 'inherit'})
我在互联网上找到了csv的代码。我希望得到的是从最高频率到最低频率的完整数据列表。我现在使用这个代码,python似乎完全忽略了csv部分,只是打印数据,好像我没有为csv编码。
对于我应该编码以查看我的预期结果的任何想法?
谢谢。
答案 0 :(得分:0)
由于你有一个字典,其中单词是键,它们的频率是值,DictWriter
不合适。对于共享一些公共密钥集的映射序列是有益的,用作csv的列。例如,如果您有一个dicts列表,例如您手动创建:
a_list = [{'dictionary[word]': '1', 'word': 'inherited'},
{'dictionary[word]': '1', 'word': 'inheritance'},
{'dictionary[word]': '1', 'word': 'inherit'}]
然后DictWriter
将成为工作的工具。但相反,你有一个dictionary
喜欢:
dictionary = {'inherited': 1,
'inheritance': 1,
'inherit': 1,
...: ...}
但是,您已经将(freq, word)
对的排序列表构建为aux
,这非常适合写入csv:
with open('Aeschylus.csv', 'wb') as csvfile:
header = ['frequency', 'word']
writer = csv.writer(csvfile)
writer.writerow(header)
# Note the plural method name
writer.writerows(aux)
python似乎完全忽略了csv部分而只是打印数据,好像我没有为csv编写代码。
听起来很奇怪。至少你应该得到一个文件 Aeschylus.csv 包含:
dictionary[word],word
1,inherited
1,inheritance
1,inherit
您的频率计数方法也可以改进。目前
#create dictionary by counting no of occurences of each word in list
wordfreq = [wordsFiltered.count(x) for x in wordsFiltered]
必须遍历wordsFiltered
中每个字的列表wordsFiltered
,因此 O(n²)。您可以反过来遍历文件中的单词,过滤并计算。 Python有一个专门的字典,用于计算名为Counter
的可用对象:
from __future__ import print_function
from collections import Counter
import csv
# Many ways to go about this, could for example yield from (<gen expr>)
def words(filelike):
for line in filelike:
for word in line.split():
yield word
def remove(iterable, stopwords):
stopwords = set(stopwords) # O(1) lookups instead of O(n)
for word in iterable:
if word not in stopwords:
yield word
if __name__ == '__main__':
with open("stopwords.txt") as f:
stopwords = f.read().split()
with open('Aeschylus.txt') as wordfile:
wordfreq = Counter(remove(words(wordfile), stopwords))
然后,和以前一样,打印单词及其频率,从最常见的开始:
for word, freq in wordfreq.most_common():
print(word, freq)
和/或写为csv:
# Since you're using python 2, 'wb' and no newline=''
with open('Aeschylus.csv', 'wb') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['word', 'freq'])
# If you want to keep most common order in CSV as well. Otherwise
# wordfreq.items() would do as well.
writer.writerows(wordfreq.most_common())