看看自己敲了多少代码

声明一下这是同学的想法,他写的有bug

我重写的

# 统计文本 字符 字符数 行数
import csv
from datetime import datetime
import os
import re


class Statistician(object):

def __init__(self, path, postfix="py"):
"""

:param path: 路径
:param postfix: 后缀
"""
self.path = path
self.postfix = postfix
self.file_path = []
self.size = 0
self.row = 0
self.char_table = dict()

def out_file_path(self, path):
"""
:return: 文件路径列表
"""
fs = os.listdir(path)
for f1 in fs:
tmp_path = os.path.join(path, f1)
if not os.path.isdir(tmp_path):
if re.match(r".*\.%s$" % self.postfix, tmp_path):
self.file_path.append(tmp_path)
# print('文件: %s' % tmp_path)
else:
# print('文件夹:%s' % tmp_path)
self.out_file_path(tmp_path)
return self.file_path

def get_params(self):
"""

:return: 字符数 行数 字符字典
"""
for path in statistician.out_file_path(self.path):
with open(path, "rb") as f:
row_content_list = f.readlines()
# print(row_content_list)
for row_content in row_content_list:
content = row_content.strip()
if content:
content = content.decode()
self.size += len(content)
self.row += 1
for c in content:
if c not in self.char_table.keys():
self.char_table[c] = 0
self.char_table[c] += 1
return self.size, self.row, self.char_table

def create_table(self):
data = [(word, num) for word, num in self.char_table.items()]
with open('example.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(data)


if __name__ == '__main__':
statistician = Statistician("C:/Users/gym/Desktop/dir_test")
begin_time = datetime.now()
size, row, table = statistician.get_params()
statistician.create_table()
end_time = datetime.now()
print(end_time-begin_time)
print("字节数:" + str(size))
print("行数:" + str(row))
print(table)

转载于:https://www.cnblogs.com/whigym/p/9914833.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>