面向对象学习笔记8----文件信息处理类

本文介绍了一个Python类,用于统计文件中的各种字符类型数量,包括数字、非空白和空白字符,同时提供文件行数和路径信息。通过继承,增加了按行读取内容的功能,并实现了统计信息的统一打印。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写一个类,能够统计某个文件的纯数字字符个数
统计非空白字符个数
空白字符个数
文件行数
文件所在路径
通过继承方式,增加一个方法,打印所有的统计信息
附加功能:
自定义一个类方法和静态方法

import os.path
class FileInfo(object):
   
    """统计文件的数字字符个数、
       非空白字符个数、
       空白字符个数、
       文件行数、
       文件所在路径"""

    def __init__(self,file_path,encoding_type="utf-8"):
        self.file_path = file_path
        self.encoding_type = encoding_type

    def count_number_str(self):
        if not os.path.exists(self.file_path):
            return 0
        count = 0
        with open(self.file_path,encoding=self.encoding_type) as fp:
            for i in fp:
                for j in i:
                    if j>="0" and j<="9":
                        count+=1
        return count
 
    def count_not_space_str(self):
        if not os.path.exists(self.file_path):
            return 0
        count = 0
        with open(self.file_path,encoding=self.encoding_type) as fp:
            for i in fp:
                for j in i:
                    if not j.isspace():
                        count+=1
        return count

    def count_space_str(self):
        if not os.path.exists(self.file_path):
            return 0
        count = 0
        with open(self.file_path,encoding=self.encoding_type) as fp:
            for i in fp:
                for j in i:
                    if j.isspace():
                        count+=1
        return count

    def count_lines(self):
        if not os.path.exists(self.file_path):
            return 0
        count = 0
        with open(self.file_path,encoding=self.encoding_type) as fp:
            for i in fp:
                count+=1                        
        return count

fi = FileInfo("f:\\a.txt")
print(fi.count_number_str())
print(fi.count_not_space_str())
print(fi.count_space_str())
print(fi.count_lines())

在这里插入图片描述
代码优化:

import os.path
class FileInfo(object):
   
    """统计文件的数字字符个数、
       非空白字符个数、
       空白字符个数、
       文件行数、
       文件所在路径"""

    def __init__(self,file_path,encoding_type="utf-8"):
        self.file_path = file_path
        self.encoding_type = encoding_type
        while 1:
            if not os.path.exists(self.file_path):
                self.file_path = input("实例化的文件路径不存在,请重新输入:")
            else:
                break

    def get_file_content(self):
        content = ""
        with open(self.file_path,encoding=self.encoding_type) as fp:
            for i in fp:
                for j in i:
                    content+=j
        return content

    def count_number_str(self):

        """统计文件中的数字字符个数"""

        count = 0
        content = self.get_file_content()
        for i in content:
            if i>="0" and i<="9":
                count+=1
        return count
 
    def count_not_space_str(self):

        """统计文件中的非空白字符个数"""

        count = 0
        content = self.get_file_content()
        for i in content:
            if not i.isspace():
                count+=1
        return count

    def count_space_str(self):

        """统计文件中的空白字符个数"""

        count = 0
        content = self.get_file_content()
        for i in content:
            if i.isspace():
                count+=1
        return count

    def count_lines(self):

        """统计文件中的行数"""

        count = 0
        content = self.get_file_content()
        for i in content.split("\n"):
            count+=1
        return count

fi = FileInfo("f:\\axxxx.txt")
print(fi.count_number_str())
print(fi.count_not_space_str())
print(fi.count_space_str())
print(fi.count_lines())

在这里插入图片描述

import os.path
class FileInfo(object):
   
    """统计文件的数字字符个数、
       非空白字符个数、
       空白字符个数、
       文件行数、
       文件所在路径"""

    def __init__(self,file_path,encoding_type="utf-8"):
        self.file_path = file_path
        self.encoding_type = encoding_type
        while 1:
            if not os.path.exists(self.file_path):
                self.file_path = input("实例化的文件路径不存在,请重新输入:")
            else:
                break

    def get_file_content(self):
        content = ""
        with open(self.file_path,encoding=self.encoding_type) as fp:
            for i in fp:
                for j in i:
                    content+=j
        return content

    def count_number_str(self):

        """统计文件中的数字字符个数"""

        count = 0
        content = self.get_file_content()
        for i in content:
            if i>="0" and i<="9":
                count+=1
        return count
 
    def count_not_space_str(self):

        """统计文件中的非空白字符个数"""

        count = 0
        content = self.get_file_content()
        for i in content:
            if not i.isspace():
                count+=1
        return count

    def count_space_str(self):

        """统计文件中的空白字符个数"""

        count = 0
        content = self.get_file_content()
        for i in content:
            if i.isspace():
                count+=1
        return count

    def count_lines(self):

        """统计文件中的行数"""

        count = 0
        content = self.get_file_content()
        for i in content.split("\n"):
            count+=1
        return count

class Advanced_FileInfo(FileInfo):

    """高级的文件信息处理类"""

    def __init__(self,file_path,encoding_type="utf-8"):
        FileInfo.__init__(self,file_path,encoding_type="utf-8")

    def get_content_by_line_num(self,line_number):
        try:
            return self.get_file_content().split("\n")[line_number-1]
        except:
            return None

    def print_file_info(self):
        print("文件的统计信息如下:")
        print("文件中包含的数字数量:%s" %self.count_number_str())
        print("文件中包含的非空白字符数量:%s" %self.count_not_space_str())
        print("文件中包含的空白字符数量:%s" %self.count_space_str())
        print("文件中包含的行数:%s" %self.count_lines())

fi = Advanced_FileInfo("f:\\a.txt")
print("获取第一行的文件内容:",fi.get_content_by_line_num(1))
fi.print_file_info()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值