python词频统计结果写入csv,Python中CSV列的词频

我有一个.csv文件,其中有一列我收集的消息,我希望得到该列中每个单词的词频列表。这是我到目前为止的情况,我不知道我在哪里犯了错误,任何帮助都将不胜感激。编辑:预期的输出是将单词的整个列表及其计数(不重复)写入另一个.csv文件。在import csv

from collections import Counter

from collections import defaultdict

output_file = 'comments_word_freqency.csv'

input_stream = open('comments.csv')

reader = csv.reader(input_stream, delimiter=',')

reader.next() #skip header

csvrow = [row[3] for row in reader] #Get the fourth column only

with open(output_file, 'rb') as csvfile:

for row in reader:

freq_dict = defaultdict(int) # the "int" part

# means that the VALUES of the dictionary are integers.

for line in csvrow:

words = line.split(" ")

for word in words:

word = word.lower() # ignores case type

freq_dict[word] += 1

writer = csv.writer(open(output_file, "wb+")) # this is what lets you write the csv file.

for key, value in freq_dict.items():

# this iterates through your dictionary and writes each pair as its own line.

writer.writerow([key, value])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值