我绝对是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编写代码一样。
知道我应该编写什么代码才能看到预期的结果吗?
谢谢。
发布于 2017-04-21 11:40:15
因为您有一个字典,其中的单词是键,它们的频率是值,所以DictWriter是不合适的。对于共享一些公共键集(用作csv的列)的映射序列来说,这是很好的。例如,如果您有一个字典列表,比如手动创建:
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())https://stackoverflow.com/questions/43540959
复制相似问题