python统计运行次数_Python统计数据的频率

2019独角兽企业重金招聘Python工程师标准>>>

35bf71b4b7ad4a72bcb9c6561b5679b1.jpg

# -*- coding: UTF-8 -*-

#!/usr/bin/env python

from collections import Counter

import collections

import jieba.analyse

import jieba

import time

import re

import sys

#去除停用词

#stopwords = {}.fromkeys(['的', '包括', '等', '是'])

stopwords = {}.fromkeys([ line.strip() for line in open("stopwords.txt") ])

#读取文件路径

bill_path = r'article_nohtml.txt'

#写入文件路径

bill_result_path = r'result.txt'

#读取文件

with open(bill_path,'r') as fr:

all_the_text = fr.read()

#处理特殊字符

all_the_text = re.sub("\"|,|\.", "", all_the_text)

#分词

data = jieba.cut(all_the_text)

#计算频率

data = dict(Counter(data))

#以词频排序

def sort_by_count(d):

#字典排序

d = collections.OrderedDict(sorted(d.items(), key = lambda t: -t[1]))

return d

data = sort_by_count(data)

#将结果集写入文件

with open(bill_result_path,'w') as fw:

for k,v in data.items():

k = k.encode('utf-8')

#处理停用词

if k not in stopwords:

#写入结果

#fw.write(str(k)+':'+str(v)+'\n')

#fw.write("%s,%d\n" % (k,v))

fw.write(str(k)+':%d'%v + '\n')

#关闭流

fw.close()

运行结果图

56c00fc2888046dcbee2715d9d818e25.png

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值