python应用案例

本文介绍了Python在文件操作方面的几个实用案例,包括统计目录下txt文件的高频单词、批量调整图片分辨率、计算py文件代码行数,以及利用goose库抓取网页正文内容。
摘要由CSDN通过智能技术生成

1、统计目录下多个txt文件,找出出现频率最多的单词

#coding=utf-8

##目录下多个txt文件,找出出现频率最多的单词
import os,re
from collections import Counter

FILESOURECE = "D://pytest"

#过滤词
stop_word  = ['the','in','of','and','to','has','that','s','is','are','a','with','as','an']

def getCounter(articlefileresource):
	'tdw'
	pattern = r'''[A-Za-z]+|\$?\d+%?$'''
	with open(articlefileresource) as f:
		r = re.findall(pattern, f.read())
		return Counter(r)



def getRun(FILE_PATH):
	os.chdir(FILE_PATH)
	total_counter=Counter()
	print os.listdir(os.getcwd())
	for i in os.listdir(os.getcwd()):
		if os.path.splitext(i)[1] == '.txt':
			total_counter += getCounter(i)

	#排除过滤出
	for i in stop_word:
		total_counter[i] = 0
	print total_counter.most_common()[0][0]


print getR
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值