繁体   English   中英

如何将字典值转换为csv文件?

[英]How to convert dictionary values into a csv file?

我是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编写代码一样。

我应该编写什么代码才能看到预期的结果?

谢谢。

由于您有一本字典,其中单词是键,而它们的频率是值,因此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())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM