【Python】Python的Common工具包-功能大全


一、功能描述

该公共包(CommonTool.py),把常用的基本功能做了封装,方便使用。

二、详细说明

使用方法

import导入该包即可:

from TzqUtils.CommonTool import *

其中“TzqUtils”是目录名字“CommonTool”是包名

2.1、文件相关功能清单

文件相关的功能涉及:

2.1.1、重写文本文件内容,覆盖内容
2.1.2、追加写入文本文件内容
2.1.3、读取文本文件所有内容
2.1.4、清空文本文件内容
2.1.5、获取文本文件编码类型 (二进制方式读取,获取字节数据,检测类型)
2.1.6、修改文本文件的编码为utf-8
2.1.7、文本文件中替换windows的换行符(\r\n)为Linux的换行符(\n)

2.2、处理字符串相关功能清单

2.2.1、查找字符串在list中是否存在,存在则返回第一个字符串所在的位置,忽略大小写
2.2.2、查找字符串在list中是否存在,存在则返回那个存在的字符串,忽略大小写
2.2.3、字符串处理,根据字符,拆分成list,去除空值
2.2.4、字符串拆分排序再合并 String split, sort, and merge
2.2.5、从字符串中提取出一连串的数字

2.3、其他功能

2.3.1、判断整型是否为空

三、完整代码

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re
import chardet
import os


class CommonTool:
    """
    公共工具类
    """

    def __init__(self):
        pass

    # 重写文本文件内容,覆盖内容
    @staticmethod
    def rewrite_file_content_f(filename: str, content: str):
        file = open(filename, 'w', encoding='utf-8')
        file.write(content)
        file.close()
        CommonTool.replace_with_linux_line_breaks_f(filename)

    # 追加写入文本文件内容
    @staticmethod
    def append_file_content_f(filename, content):
        # 打开文件
        file = open(filename, 'a', encoding='utf-8')
        # 追加内容
        file.write(content)
        # 关闭文件
        file.close()
        CommonTool.replace_with_linux_line_breaks_f(filename)

    # 读取文本文件所有内容
    @staticmethod
    def get_file_content_f(file_path: str):
        coding = CommonTool.get_encoding_f(file_path)
        with open(file_path, 'r', encoding=coding) as f:
            text = f.read()
        f.close()
        return text

    # 清空文本文件内容
    @staticmethod
    def clear_file_content_f(filename: str):
        file = open(filename, 'w', encoding='utf-8')
        file.write("")
        file.close()
        return None

    # 获取文本文件编码类型 (二进制方式读取,获取字节数据,检测类型)
    @staticmethod
    def get_encoding_f(file: str):
        with open(file, 'rb') as f:
            file_encoding_tmp_string = chardet.detect(f.read())['encoding']
        f.close()
        return file_encoding_tmp_string

    # 修改文本文件的编码为utf-8
    @staticmethod
    def change_file_encode_to_utf8_f(file_path: str):
        coding = CommonTool.get_encoding_f(file_path)
        with open(file_path, 'r', encoding=coding) as f:
            text = f.read()
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(text)
        f.close()

    # 文本文件中替换windows的换行符(\r\n)为Linux的换行符(\n)
    @staticmethod
    def replace_with_linux_line_breaks_f(file_full_path: str):
        with open(file_full_path, "rb") as f:
            data = bytearray(os.path.getsize(file_full_path))
            f.readinto(data)
            data = data.replace(b"\r\n", b"\n")
        with open(file_full_path, "wb") as f:
            f.write(data)
        f.close()

    # 查找字符串在list中是否存在,存在则返回第一个字符串所在的位置,忽略大小写
    @staticmethod
    def find_element_position_f(string_i: str, list_i: list):
        string_i = string_i.lower()
        # 使用Python内置列表推导式把list所有元素替换为小写
        list_i = [x.lower() for x in list_i]
        for element in list_i:
            if element in string_i:
                return string_i.index(element)  # 返回找到的第一个元素的位置
        return -1  # 如果没有找到任何匹配的元素,则返回-1

    # 查找字符串在list中是否存在,存在则返回那个存在的字符串,忽略大小写
    @staticmethod
    def find_element_f(string_i: str, list_i: list):
        string_i = string_i.lower()
        # 使用Python内置列表推导式把list所有元素替换为小写
        list_i = [x.lower() for x in list_i]
        # print("string_i = “" + string_i + "”")
        # print("lst = “" + str(lst) + "”")
        for element in list_i:
            if element in string_i:
                return element
        return -1

    # 字符串处理,根据字符,拆分成list,去除空值
    @staticmethod
    def string_split_f(string_i: str, split_string_i: str):
        # 字符串拆分:根据 split_string 拆分
        tmp_list = string_i.split(split_string_i)
        # 使用map函数去除list里面各项的首尾空格
        tmp_list = list(map(str.strip, tmp_list))
        # 使用列表推导式去除空值
        tmp_list = [item for item in tmp_list if item]
        # print(tmp_list)
        return tmp_list

    # 字符串拆分排序再合并 String split, sort, and merge
    @staticmethod
    def string_split_sort_and_merge_f(string_i: str):
        split_list = string_i.split(",")  # 拆分字符串为列表
        sorted_list = sorted(split_list)  # 对列表进行排序
        merged_string = ",".join(sorted_list)  # 合并为一个字符串
        return merged_string

    # 从字符串中提取出一连串的数字
    @staticmethod
    def get_numbers_from_string_f(input_string: str):
        nums = re.findall(r'\d+', input_string)
        if nums:
            return nums[0]
        else:
            return None

    # 判断整型是否为空
    @staticmethod
    def is_number_empty(input_number_i: int):
        return input_number_i is None


if __name__ == '__main__':
    pass

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tzq@2018

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值