• [技术干货] Python3 hashlib 模块基本用法详解【转载】
    一、基本用法hashlib的核心流程是:创建哈希对象 → 更新数据 → 获取哈希值。关键说明:哈希算法处理的是字节数据(bytes),因此字符串需先通过encode()编码为 bytes(如utf-8)。可通过update()多次传入数据,最终计算的是所有数据的总哈希值(适合分块处理大数据)。哈希值可通过digest()(返回 bytes 类型)或hexdigest()(返回十六进制字符串,更易读)获取。二、常用哈希算法及示例hashlib支持多种主流算法,如md5、sha1、sha256、sha512等(安全性:sha512 > sha256 > sha1 > md5)。1. MD512345678import hashlib# 1. 创建MD5哈希对象md5_hash = hashlib.md5()# 2. 更新数据(需传入bytes类型)data = "hello world"md5_hash.update(data.encode("utf-8"))  # 字符串编码为bytes# 3. 获取哈希值(十六进制字符串)print(md5_hash.hexdigest())  # 输出:5eb63bbbe01eeed093cb22bb8f5acdc32. SHA-25612345import hashlib# 直接传入bytes数据(b前缀表示bytes)sha256_hash = hashlib.sha256(b"hello world")# 获取哈希值print(sha256_hash.hexdigest())  # 输出:a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e44b03. 分块更新数据(适合大文件)对于大文件或流式数据,可分多次调用update()累积数据,避免一次性加载到内存:1234567891011import hashlibdef calculate_file_hash(file_path, algorithm="sha256"):    # 创建指定算法的哈希对象(通过hashlib.new()支持动态指定算法)    hash_obj = hashlib.new(algorithm)    # 分块读取文件并更新哈希    with open(file_path, "rb") as f:        while chunk := f.read(4096):  # 每次读取4096字节(可调整)            hash_obj.update(chunk)    return hash_obj.hexdigest()# 示例:计算某文件的SHA-256哈希print(calculate_file_hash("large_file.zip"))  # 输出文件的哈希值三、重要属性与方法方法 / 属性说明hashlib.new(algorithm)创建指定算法的哈希对象(如new("sha3_256"))update(data)向哈希对象传入数据(data 必须为 bytes)digest()返回 bytes 类型的哈希值hexdigest()返回十六进制字符串类型的哈希值digest_size哈希值的字节长度(如 SHA-256 为 32 字节)block_size算法的块大小(用于内部计算)algorithms_available系统支持的所有哈希算法列表algorithms_guaranteed所有 Python 环境都保证支持的算法列表四、注意事项安全性:MD5和SHA1已被证明存在安全漏洞,不推荐用于密码存储、数字签名等安全场景,建议使用SHA-256、SHA-512或更先进的SHA3系列。数据类型:必须传入 bytes 类型数据,字符串需先编码(如str.encode("utf-8")),否则会报错。哈希碰撞:理论上不同数据可能产生相同哈希值(碰撞),但好的算法(如 SHA-256)碰撞概率极低,可忽略。五、扩展:带密钥的哈希(HMAC)hashlib常与hmac模块配合使用,实现带密钥的哈希(HMAC),用于身份验证等场景(如 API 签名):1234567import hmacimport hashlibkey = b"my_secret_key"  # 密钥(bytes类型)data = b"hello world"   # 待加密数据# 计算HMAC-SHA256hmac_obj = hmac.new(key, data, digestmod=hashlib.sha256)print(hmac_obj.hexdigest())  # 输出带密钥的哈希值 通过hashlib,可以便捷地实现数据哈希计算,满足数据校验、安全存储等需求。实际使用时需根据场景选择合适的算法,并注意数据类型和安全性要求。
  • [技术干货] Python获取文件大小的三种主流方法【转载】
    一、核心方法解析1. os模块的getsize()函数1234567891011import os # 获取文件路径file_path = "test_file.txt" try:    # 获取文件大小(字节)    size_bytes = os.path.getsize(file_path)    print(f"文件大小(os模块):{size_bytes} 字节")except FileNotFoundError:    print(f"错误:文件 {file_path} 不存在")输出示例1文件大小(os模块):24 字节2. pathlib模块的stat()方法123456789101112from pathlib import Path # 创建Path对象file_path = Path("test_file.txt") try:    # 获取文件状态信息    file_stat = file_path.stat()    size_bytes = file_stat.st_size    print(f"文件大小(pathlib模块):{size_bytes} 字节")except FileNotFoundError:    print(f"错误:文件 {file_path} 不存在")3. 格式化文件大小(字节转可读格式)12345678910111213def format_size(size_bytes):    units = ['B', 'KB', 'MB', 'GB']    unit_index = 0         while size_bytes >= 1024 and unit_index < len(units)-1:        size_bytes /= 1024        unit_index += 1             return f"{size_bytes:.2f} {units[unit_index]}" # 示例使用print(f"格式化后大小:{format_size(24)}")  # 输出:24.00 Bprint(f"格式化后大小:{format_size(1500)}")  # 输出:1.46 KB二、关键注意事项异常处理必须使用try-except处理FileNotFoundError等异常推荐使用OSError捕获更广泛的系统错误大文件处理Python的整数类型自动支持大文件(>2GB)无需特殊处理32位系统兼容性问题符号链接处理默认获取的是链接指向的目标文件大小如需获取链接本身大小,需使用特殊方法(Linux系统)三、性能对比在10万次调用测试中:方法执行时间(秒)内存占用os.path.getsize0.89低pathlib.stat()1.05中四、应用场景建议简单脚本:优先使用os模块方案面向对象项目:推荐pathlib方案需要格式化输出:使用format_size函数五、扩展应用递归获取目录总大小123456789def get_dir_size(path='.'):    total_size = 0    for dirpath, dirnames, filenames in os.walk(path):        for f in filenames:            fp = os.path.join(dirpath, f)            total_size += os.path.getsize(fp)    return total_size print(f"当前目录总大小:{format_size(get_dir_size())}")通过本文介绍的三种方法,开发者可以灵活应对不同场景下的文件大小获取需求。建议在实际开发中先进行小范围测试,确认目标文件存在性和访问权限后,再决定最终采用的技术方案。
  • [技术干货] Python获取文件创建时间的三种方法【转载】
    一、核心方法解析1. os模块的getctime()函数123456789101112import osimport time # 获取文件路径file_path = "test_file.txt" # 获取创建时间戳ctime_timestamp = os.path.getctime(file_path) # 转换为本地时间格式ctime_formatted = time.ctime(ctime_timestamp)print(f"文件创建时间(os模块):{ctime_formatted}")输出示例1文件创建时间(os模块):Wed Oct 22 20:12:09 20252. pathlib模块的stat()方法123456789101112from pathlib import Pathimport time # 创建Path对象file_path = Path("test_file.txt") # 获取文件状态信息file_stat = file_path.stat() # 提取创建时间并格式化ctime_pathlib = file_stat.st_ctimeprint(f"文件创建时间(pathlib模块):{time.ctime(ctime_pathlib)}")3. 平台适配方案1234567891011121314151617import platformimport os def get_creation_time(file_path):    system = platform.system()         if system == 'Windows':        return os.path.getctime(file_path)    elif system == 'Linux' or system == 'Darwin':  # Darwin对应macOS        stat = os.stat(file_path)        if hasattr(stat, 'st_birthtime'):  # macOS特有属性            return stat.st_birthtime        else:            # Linux通常用修改时间近似处理            return stat.st_mtime    else:        raise NotImplementedError(f"不支持的操作系统: {system}")二、关键注意事项操作系统差异Windows系统:直接支持st_ctime(创建时间)macOS系统:使用st_birthtime属性Linux系统:通常没有精确创建时间,建议用修改时间st_mtime替代时区处理默认返回的是UTC时间戳,如需本地时间:12from datetime import datetimelocal_time = datetime.fromtimestamp(ctime_timestamp)异常处理最佳实践123456try:    ctime = os.path.getctime(file_path)except FileNotFoundError:    print(f"错误:文件 {file_path} 不存在")except OSError as e:    print(f"系统错误:{str(e)}")三、性能对比在10万次调用测试中:方法执行时间(秒)内存占用os.path.getctime1.23低pathlib.stat()1.58中平台适配方案1.72高四、应用场景建议批量文件处理:优先使用os模块方案跨平台项目:采用适配函数方案面向对象项目:推荐pathlib方案
  • [技术干货] 使用Python轻松实现裁剪PDF并导出为图片【转载】
    使用 Python 裁剪 PDF 页面在 Python 中,我们可以使用 Spire.PDF 库来裁剪 PDF 页面。该库功能强大,支持裁剪、旋转、导出图片等操作,本文将以它为主要示例。1. 安装依赖首先,需要在终端运行以下命令安装 Spire.PDF 库:1pip install spire.pdf2. 基本裁剪操作裁剪 PDF 页面主要是调整页面的显示区域,也就是设置页面的裁剪框(crop box)。以下是一个简单示例:123456789101112131415161718from spire.pdf.common import *from spire.pdf import * # 创建 PdfDocument 对象pdf = PdfDocument() # 从磁盘加载 PDF 文件pdf.LoadFromFile("输入.pdf") # 获取第一页page = pdf.Pages[0] # 设置裁剪区域 (x, y, width, height)page.CropBox = RectangleF(0.0, 300.0, 600.0, 260.0) # 保存裁剪后的 PDFpdf.SaveToFile("裁剪.pdf")pdf.Close()代码说明:RectangleF(0.0, 300.0, 600.0, 260.0) 定义裁剪区域的左上角坐标和宽高。page.CropBox 会将页面裁剪为指定矩形区域。保存后的 PDF 文件只保留裁剪区域内容。裁剪 PDF 并导出为图片有时我们不仅希望裁剪 PDF,还希望将裁剪后的页面导出为图片。Spire.PDF 可以直接将 PDF 页面渲染为图片,支持保存为多种图片格式。1. 裁剪单页并导出为图片1234567891011121314151617181920from spire.pdf.common import *from spire.pdf import * # 创建 PdfDocument 对象pdf = PdfDocument() # 从磁盘加载 PDF 文件pdf.LoadFromFile("输入.pdf") # 获取第一页page = pdf.Pages[0] # 设置裁剪区域page.CropBox = RectangleF(0.0, 300.0, 600.0, 260.0) # 将页面转换为图片并保存with pdf.SaveAsImage(0) as imageS:    imageS.Save("裁剪.png") pdf.Close()代码说明:pdf.SaveAsImage(0) 将 PDF 的第一页渲染为图片。使用 with 语句可以确保图片资源正确关闭。imageS.Save("文件名.png") 保存为 PNG 格式,也可改为 JPEG 等。2. 批量裁剪所有页面并导出为图片如果 PDF 文件有多页,需要裁剪并导出为图片,可以使用索引循环:123456789101112131415161718from spire.pdf.common import *from spire.pdf import * pdf = PdfDocument()pdf.LoadFromFile("示例.pdf") # 通过索引遍历页面for i in range(len(pdf.Pages)):    page = pdf.Pages[i]         # 设置裁剪区域    page.CropBox = RectangleF(0.0, 300.0, 600.0, 260.0)         # 导出图片    with pdf.SaveAsImage(i) as img:        img.Save(f"输出/页面-{i+1}.png") pdf.Close() 这样,每一页都能裁剪并保存为独立图片,便于后续使用或发布。
  • [技术干货] python 判断对象类型
    在 Python 中,判断对象类型有多种方法,以下是常用的几种方式及其适用场景:1. type() 函数直接获取对象的类型,严格匹配(不考虑继承关系)。x = 42 if type(x) == int: print("x is an int") # 输出 ⚠️ 注意:type() 不会考虑继承关系。例如,如果 class MyInt(int): pass,type(MyInt()) == int 会返回 False。2. isinstance() 函数检查对象是否是某个类或其子类的实例,支持继承关系。x = 42 if isinstance(x, int): print("x is an int or its subclass") # 输出 class MyInt(int): pass y = MyInt() if isinstance(y, int): print("y is a subclass of int") # 输出 ✅ 推荐:大多数情况下优先使用 isinstance(),尤其是需要兼容子类时。3. issubclass() 函数检查一个类是否是另一个类的子类。class Parent: pass class Child(Parent): pass print(issubclass(Child, Parent)) # 输出: True 4. hasattr() 函数检查对象是否有某个属性或方法(不直接判断类型,但常用于鸭子类型判断)。x = "hello" if hasattr(x, "upper"): print("x has 'upper' method") # 输出 5. __class__ 属性获取对象的类(与 type() 类似,但可通过属性访问)。x = [1, 2, 3] print(x.__class__) # 输出: <class 'list'> 6. callable() 函数检查对象是否可调用(如函数、类、实现了 __call__ 的对象)。def func(): pass print(callable(func)) # 输出: True print(callable(list)) # 输出: True(类是可调用的) 7. 抽象基类(abc 模块)通过注册的方式检查对象是否符合特定接口(更灵活的类型检查)。from collections.abc import Sequence x = [1, 2, 3] print(isinstance(x, Sequence)) # 输出: True(list 是 Sequence 的子类) 8. 类型注解与 typing 模块(Python 3.8+)结合 typing 和 isinstance() 进行复杂类型检查。from typing import List, Union def process_data(data: Union[List[int], str]): if isinstance(data, list): print("Data is a list of integers") elif isinstance(data, str): print("Data is a string") 常见场景示例判断字典或列表data = {"key": "value"} if isinstance(data, dict): print("This is a dictionary") elif isinstance(data, list): print("This is a list") 判断数字类型def check_number(x): if isinstance(x, (int, float)): print("x is a number") 严格类型检查(排除子类)if type(x) is int: # 使用 `is` 严格匹配 print("x is exactly an int, not a subclass") 总结方法特点type(obj)严格匹配类型,不考虑继承。isinstance(obj, cls)支持继承关系,推荐日常使用。issubclass(A, B)检查类之间的继承关系。hasattr(obj, attr)检查属性或方法,用于鸭子类型。abc 模块定义抽象接口,更灵活的类型约束。根据需求选择合适的方法:优先用 isinstance(),除非明确需要严格类型匹配。
  • [技术干货] Pydantic Enum 转 字符串
    在 Pydantic 中,当你使用 str 和 Enum 的子类(如你的 EmbeddingModelProvider)时,可以通过访问枚举成员的 .value 属性来获取其字符串值,然后可以在 f-string 中使用。示例代码from enum import Enum from pydantic import BaseModel class EmbeddingModelProvider(str, Enum): OPENAI = "openai" COHERE = "cohere" HUGGINGFACE = "huggingface" # 示例:在 f-string 中使用枚举值 provider = EmbeddingModelProvider.OPENAI f_string_example = f"Selected provider: {provider.value}" print(f_string_example) # 输出: Selected provider: openai 关键点说明.value 属性:由于 EmbeddingModelProvider 继承自 str 和 Enum,每个枚举成员的 .value 就是其字符串值(如 OPENAI.value 返回 "openai")。直接使用枚举成员:在 f-string 中,{provider} 会调用枚举成员的 __str__ 方法,默认返回 .value(因为继承自 str),所以也可以直接写:f_string_example = f"Selected provider: {provider}" 在 Pydantic 模型中使用:如果枚举是模型字段,可以通过模型实例访问字段的 .value:class Config(BaseModel): provider: EmbeddingModelProvider config = Config(provider=EmbeddingModelProvider.COHERE) print(f"Provider: {config.provider.value}") # 输出: Provider: cohere 总结推荐方式:直接使用 {provider.value} 或 {provider}(因继承自 str,两者效果相同)。确保枚举类继承 str,这样 Pydantic 会自动将枚举值序列化为字符串。
  • [技术干货] python pathlib 获取文件名
    在 Python 中,使用 pathlib 模块可以非常方便地获取文件名(包括不带扩展名的文件名、扩展名等)。以下是常见操作:1. 获取文件名(带扩展名)使用 Path.name 属性:from pathlib import Path file_path = Path("/path/to/your/file/example.txt") print(file_path.name) # 输出: example.txt 2. 获取文件名(不带扩展名)使用 Path.stem 属性:from pathlib import Path file_path = Path("/path/to/your/file/example.txt") print(file_path.stem) # 输出: example 3. 获取文件扩展名使用 Path.suffix(单个扩展名)或 Path.suffixes(多个扩展名列表):from pathlib import Path file_path = Path("/path/to/your/file/example.tar.gz") print(file_path.suffix) # 输出: .gz(最后一个扩展名) print(file_path.suffixes) # 输出: ['.tar', '.gz'](所有扩展名列表) 4. 获取父目录路径使用 Path.parent:from pathlib import Path file_path = Path("/path/to/your/file/example.txt") print(file_path.parent) # 输出: /path/to/your/file 5. 组合路径并获取文件名pathlib 可以直接拼接路径,然后提取文件名:from pathlib import Path dir_path = Path("/path/to/your/file") file_path = dir_path / "subfolder" / "example.txt" print(file_path.name) # 输出: example.txt 6. 判断是否是文件使用 Path.is_file():from pathlib import Path file_path = Path("/path/to/your/file/example.txt") if file_path.is_file(): print(f"{file_path.name} 是一个文件") 完整示例from pathlib import Path # 定义文件路径 file_path = Path("/home/user/docs/report.pdf") # 获取文件名信息 print("完整文件名:", file_path.name) # report.pdf print("不带扩展名:", file_path.stem) # report print("扩展名:", file_path.suffix) # .pdf print("父目录:", file_path.parent) # /home/user/docs # 检查路径类型 if file_path.is_file(): print(f"{file_path.name} 是文件") 为什么推荐 pathlib?跨平台:自动处理不同操作系统(Windows/Linux/macOS)的路径分隔符(/ 或 \)。面向对象:比 os.path 更直观,支持链式操作。功能丰富:集成路径拼接、文件检查、读写等操作。如果需要兼容旧版 Python(<3.4),可以用 os.path,但新项目建议优先使用 pathlib。
  • [技术干货] python判断对象是否有某个属性
    在 Python 中,判断一个对象是否有某个属性有多种方法,以下是常见的几种方式及其适用场景:1. 使用 hasattr() 函数语法:hasattr(object, name)作用:检查对象是否有指定名称的属性(包括实例属性、类属性、继承的属性等)。示例:class MyClass: def __init__(self): self.value = 42 obj = MyClass() # 检查属性是否存在 print(hasattr(obj, "value")) # True print(hasattr(obj, "nonexistent")) # False 特点:支持动态属性名(字符串形式)。会触发 __getattr__ 或 __getattribute__ 方法(如果定义了)。2. 使用 try-except 捕获 AttributeError适用场景:直接访问属性并处理异常(更 Pythonic 的方式)。示例:class MyClass: def __init__(self): self.value = 42 obj = MyClass() try: print(obj.value) # 正常访问 except AttributeError: print("属性不存在") try: print(obj.nonexistent) # 触发 AttributeError except AttributeError: print("属性不存在") 特点:避免重复调用 hasattr() 和属性访问,性能略优。适用于需要立即使用属性值的场景。3. 使用 getattr() 的默认值语法:getattr(object, name, default=None)作用:尝试获取属性,如果不存在则返回默认值。示例:class MyClass: def __init__(self): self.value = 42 obj = MyClass() # 获取存在的属性 print(getattr(obj, "value", None)) # 42 # 获取不存在的属性(返回默认值) print(getattr(obj, "nonexistent", "default")) # "default" 特点:一行代码同时完成检查和获取。适合需要提供默认值的场景。4. 检查 __dict__ 或 vars()适用场景:仅检查实例属性(不包括类属性或继承的属性)。示例:class MyClass: def __init__(self): self.value = 42 obj = MyClass() # 检查实例属性(不包括类属性) print("value" in obj.__dict__) # True print("nonexistent" in vars(obj)) # False 注意:如果对象定义了 __slots__,__dict__ 可能不存在。不检查继承的属性或类属性。5. 使用 dir() 列出所有属性适用场景:需要查看对象的所有可用属性(包括继承的)。示例:class MyClass: def __init__(self): self.value = 42 obj = MyClass() # 列出所有属性(包括继承的) print("value" in dir(obj)) # True print("nonexistent" in dir(obj)) # False 特点:返回属性名列表(包括方法、继承的属性等)。性能较差,通常用于调试。总结对比方法是否触发 __getattr__检查继承属性性能适用场景hasattr()✅ 是✅ 是中通用检查try-except❌ 否✅ 是高需要立即访问属性时getattr()✅ 是✅ 是中需要默认值时__dict__/vars()❌ 否❌ 否高仅检查实例属性dir()❌ 否✅ 是低调试或列出所有属性最佳实践建议优先使用 try-except:如果需要立即使用属性值,直接访问并捕获 AttributeError 是最 Pythonic 的方式。try: value = obj.attribute except AttributeError: value = "default" 需要默认值时用 getattr():value = getattr(obj, "attribute", "default") 避免频繁调用 hasattr():如果后续需要访问属性,重复调用 hasattr() + 属性访问会导致性能浪费。谨慎使用 __dict__:仅当明确需要排除继承属性时使用,且注意 __slots__ 的影响。示例:综合判断属性是否存在并处理class User: def __init__(self, name): self.name = name def greet(self): return f"Hello, {self.name}!" def safe_access(obj, attr, default=None): # 方法1: hasattr + getattr if hasattr(obj, attr): return getattr(obj, attr) return default # 方法2: 更简洁的写法(推荐) # return getattr(obj, attr, default) user = User("Alice") # 检查实例属性 print(safe_access(user, "name")) # "Alice" # 检查方法 print(safe_access(user, "greet")) # <bound method User.greet> # 检查不存在的属性 print(safe_access(user, "age", 18)) # 18
  • [技术干货] 【合集】9月技术干货汇总
    Python、GO、Git、 AJAX、TensorFlow、 Spring相关技术干货合集: 1;Git拉取代码的六种高效方式 【转载】cid:link_3 2; AJAX表单验证项目实战之实时用户名检查功能【转载】cid:link_4 3;TensorFlow 张量操作的实现【转载】cid:link_5 4; Python从Word文档中提取图片并生成PPT的操作代码【转载】cid:link_0 5; GO语言短变量声明的实现示例【转载】cid:link_6 6;GO语言中函数命名返回值的使用【转载】cid:link_7 7; Python实现固定列宽文本格式化终极指南【转载】cid:link_1 8; Python字典深度比较之如何高效寻找两个字典的相同点【转载】cid:link_8 9;Go语言结合GORM实现数据库事务处理的示例代码【转载】cid:link_9 10;PyTorch中数据加载器错误的报错与修复指南【转载】cid:link_2 11; python将word的doc另存为docx的实现方案【转载】cid:link_10 12;Git Commit生成与合入Patch指南分享【转载】cid:link_11 13; Spring通过拦截器实现多数据源切换的示例代码【转载】cid:link_12 
  • [技术干货] python将word的doc另存为docx的实现方案【转载】
    方案 1:直接保存为 DOCX(如果已经是 DOCX 格式)如果你实际上是想将一个 DOCX 文件另存为另一个 DOCX 文件(例如进行一些修改后保存),可以这样做:123456789from docx import Document # 打开现有的 DOCX 文件doc = Document('input.docx') # 进行任何需要的修改... # 另存为新的 DOCX 文件doc.save('output.docx')方案 2:将 DOC 转换为 DOCX如果你确实需要将旧的 .doc 格式转换为 .docx 格式,你需要使用其他工具,因为 python-docx 不能直接读取 .doc 文件。以下是几种方法:方法 1:使用 win32com(仅 Windows)1234567891011import win32com.client def convert_doc_to_docx(doc_path, docx_path):    word = win32com.client.Dispatch("Word.Application")    doc = word.Documents.Open(doc_path)    doc.SaveAs(docx_path, FileFormat=16)  # 16 是 DOCX 格式    doc.Close()    word.Quit() # 使用示例convert_doc_to_docx('input.doc', 'output.docx')方法 2:使用 pypandoc(需要安装 Pandoc)12345678import pypandoc def convert_doc_to_docx(doc_path, docx_path):    output = pypandoc.convert_file(doc_path, 'docx', outputfile=docx_path)    assert output == ""  # 确保转换成功 # 使用示例convert_doc_to_docx('input.doc', 'output.docx')方法 3:使用 LibreOffice 命令行(跨平台)1234567import subprocess def convert_doc_to_docx(doc_path, docx_path):    subprocess.run(['libreoffice', '--headless', '--convert-to', 'docx', doc_path, '--outdir', output_dir]) # 使用示例convert_doc_to_docx('input.doc', 'output.docx')注意事项对于 .doc 到 .docx 的转换,win32com 方法需要安装 Microsoft Wordpypandoc 方法需要先安装 PandocLibreOffice 方法需要安装 LibreOffice转换后最好检查文档格式是否正确保留
  • [技术干货] Python字典深度比较之如何高效寻找两个字典的相同点【转载】
    一、基础比较方法1.1 键的比较操作1234567891011121314dict1 = {'a': 1, 'b': 2, 'c': 3}dict2 = {'b': 20, 'c': 3, 'd': 4} # 找出共同键common_keys = dict1.keys() & dict2.keys()print("共同键:", common_keys)  # {'b', 'c'} # 找出dict1独有的键unique_to_dict1 = dict1.keys() - dict2.keys()print("dict1独有键:", unique_to_dict1)  # {'a'} # 找出dict2独有的键unique_to_dict2 = dict2.keys() - dict1.keys()print("dict2独有键:", unique_to_dict2)  # {'d'}1.2 键值对比较1234567891011# 找出完全相同的键值对common_items = dict1.items() & dict2.items()print("相同键值对:", common_items)  # {('c', 3)} # 找出键相同但值不同的项common_keys_diff_values = {    k: (dict1[k], dict2[k])    for k in dict1.keys() & dict2.keys()    if dict1[k] != dict2[k]}print("键同值异:", common_keys_diff_values)  # {'b': (2, 20)}1.3 值比较123456789101112# 找出共同值common_values = set(dict1.values()) & set(dict2.values())print("共同值:", common_values)  # {3} # 值相同的键映射value_key_map = {}for value in common_values:    keys1 = [k for k, v in dict1.items() if v == value]    keys2 = [k for k, v in dict2.items() if v == value]    value_key_map[value] = (keys1, keys2) print("值相同键映射:", value_key_map)  # {3: (['c'], ['c'])}二、高级比较技术2.1 嵌套字典比较1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162def compare_nested_dicts(dict1, dict2, path=""):    """递归比较嵌套字典"""    diff = {}         # 检查共同键    common_keys = set(dict1.keys()) & set(dict2.keys())         for key in common_keys:        new_path = f"{path}.{key}" if path else key                 # 值都是字典则递归比较        if isinstance(dict1[key], dict) and isinstance(dict2[key], dict):            nested_diff = compare_nested_dicts(dict1[key], dict2[key], new_path)            if nested_diff:                diff[new_path] = nested_diff        # 值相同则记录        elif dict1[key] == dict2[key]:            diff[new_path] = ("相同", dict1[key])        # 值不同则记录差异        else:            diff[new_path] = ("不同", dict1[key], dict2[key])         return diff # 使用示例config1 = {    'database': {        'host': 'db1.example.com',        'port': 3306,        'credentials': {            'user': 'admin',            'password': 'secret'        }    },    'logging': {        'level': 'INFO',        'path': '/var/log'    }} config2 = {    'database': {        'host': 'db2.example.com',        'port': 3306,        'credentials': {            'user': 'admin',            'password': 'newsecret'        }    },    'logging': {        'level': 'DEBUG',        'path': '/var/log'    },    'cache': {        'enabled': True    }} diff_report = compare_nested_dicts(config1, config2)print("嵌套字典比较结果:")import pprintpprint.pprint(diff_report)2.2 自定义比较函数1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253def advanced_dict_compare(dict1, dict2,                          key_comparator=None,                          value_comparator=None):    """支持自定义比较器的字典比较"""    # 默认比较器    key_comparator = key_comparator or (lambda x, y: x == y)    value_comparator = value_comparator or (lambda x, y: x == y)         result = {        'common_keys': [],        'common_items': [],        'key_diff': {            'only_in_dict1': [],            'only_in_dict2': []        },        'value_diff': []    }         # 键比较    all_keys = set(dict1.keys()) | set(dict2.keys())    for key in all_keys:        if key in dict1 and key in dict2:            result['common_keys'].append(key)                         # 值比较            if value_comparator(dict1[key], dict2[key]):                result['common_items'].append((key, dict1[key]))            else:                result['value_diff'].append((key, dict1[key], dict2[key]))        elif key in dict1:            result['key_diff']['only_in_dict1'].append(key)        else:            result['key_diff']['only_in_dict2'].append(key)         return result # 使用示例:浮点数容差比较def float_compare(v1, v2, tolerance=1e-5):    """浮点数容差比较"""    if isinstance(v1, float) and isinstance(v2, float):        return abs(v1 - v2) < tolerance    return v1 == v2 dict1 = {'a': 1.0, 'b': 2.00001, 'c': 3}dict2 = {'a': 1.000001, 'b': 2.0, 'd': 4} comparison = advanced_dict_compare(    dict1, dict2,     value_comparator=float_compare) print("容差比较结果:")pprint.pprint(comparison)2.3 类型无关比较1234567891011121314151617181920212223242526272829303132333435363738394041424344454647def type_agnostic_compare(dict1, dict2):    """类型无关的字典比较"""    common_keys = set(dict1.keys()) & set(dict2.keys())         same_items = {}    diff_items = {}         for key in common_keys:        # 尝试类型转换后比较        try:            val1 = dict1[key]            val2 = dict2[key]                         # 数值类型统一转float比较            if isinstance(val1, (int, float)) and isinstance(val2, (int, float)):                if float(val1) == float(val2):                    same_items[key] = val1                else:                    diff_items[key] = (val1, val2)            # 字符串类型统一转小写比较            elif isinstance(val1, str) and isinstance(val2, str):                if val1.lower() == val2.lower():                    same_items[key] = val1                else:                    diff_items[key] = (val1, val2)            # 其他类型直接比较            elif val1 == val2:                same_items[key] = val1            else:                diff_items[key] = (val1, val2)        except Exception:            diff_items[key] = (dict1[key], dict2[key])         return {        'same_items': same_items,        'diff_items': diff_items,        'unique_to_dict1': set(dict1.keys()) - common_keys,        'unique_to_dict2': set(dict2.keys()) - common_keys    } # 使用示例dict1 = {'a': 'Hello', 'b': 100, 'c': '2023-01-01'}dict2 = {'a': 'hello', 'b': '100', 'c': '2023-01-01'} result = type_agnostic_compare(dict1, dict2)print("类型无关比较结果:")pprint.pprint(result)三、性能优化策略3.1 大型字典优化12345678910111213141516171819202122232425262728293031323334353637383940414243444546def efficient_large_dict_compare(dict1, dict2):    """高效大型字典比较"""    # 第一步:键比较(使用集合操作)    keys1 = set(dict1.keys())    keys2 = set(dict2.keys())         common_keys = keys1 & keys2    unique_keys1 = keys1 - keys2    unique_keys2 = keys2 - keys1         # 第二步:并行值比较    from concurrent.futures import ThreadPoolExecutor         same_items = {}    diff_items = {}         def compare_key(key):        if dict1[key] == dict2[key]:            return (key, True, dict1[key])        else:            return (key, False, (dict1[key], dict2[key]))         with ThreadPoolExecutor() as executor:        results = executor.map(compare_key, common_keys)                 for key, is_same, value in results:            if is_same:                same_items[key] = value            else:                diff_items[key] = value         return {        'same_items': same_items,        'diff_items': diff_items,        'unique_to_dict1': unique_keys1,        'unique_to_dict2': unique_keys2    } # 生成大型字典dict_large1 = {str(i): i for i in range(100000)}dict_large2 = {str(i): i if i % 100 != 0 else i+1 for i in range(100000)} # 比较大型字典result = efficient_large_dict_compare(dict_large1, dict_large2)print(f"相同项数量: {len(result['same_items'])}")print(f"差异项数量: {len(result['diff_items'])}")3.2 惰性比较技术12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455class DictComparator:    """惰性字典比较器"""         def __init__(self, dict1, dict2):        self.dict1 = dict1        self.dict2 = dict2        self._common_keys = None        self._unique_keys1 = None        self._unique_keys2 = None         @property    def common_keys(self):        if self._common_keys is None:            self._common_keys = set(self.dict1.keys()) & set(self.dict2.keys())        return self._common_keys         @property    def unique_keys1(self):        if self._unique_keys1 is None:            self._unique_keys1 = set(self.dict1.keys()) - set(self.dict2.keys())        return self._unique_keys1         @property    def unique_keys2(self):        if self._unique_keys2 is None:            self._unique_keys2 = set(self.dict2.keys()) - set(self.dict1.keys())        return self._unique_keys2         def get_common_items(self):        """获取相同键值对"""        return {            k: self.dict1[k]            for k in self.common_keys            if self.dict1[k] == self.dict2[k]        }         def get_diff_items(self):        """获取键同值异项"""        return {            k: (self.dict1[k], self.dict2[k])            for k in self.common_keys            if self.dict1[k] != self.dict2[k]        }         def find_keys_with_value(self, value):        """查找具有特定值的键"""        keys1 = [k for k, v in self.dict1.items() if v == value]        keys2 = [k for k, v in self.dict2.items() if v == value]        return keys1, keys2 # 使用示例comparator = DictComparator(dict1, dict2)print("共同键:", comparator.common_keys)print("相同键值对:", comparator.get_common_items())print("值相同的键:", comparator.find_keys_with_value(3))四、企业级应用案例4.1 配置差异分析系统123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111class ConfigDiffAnalyzer:    """配置差异分析系统"""         def __init__(self):        self.config_history = {}         def add_config_version(self, version, config):        """添加配置版本"""        self.config_history[version] = config         def compare_versions(self, version1, version2):        """比较两个配置版本"""        if version1 not in self.config_history or version2 not in self.config_history:            raise ValueError("配置版本不存在")                 config1 = self.config_history[version1]        config2 = self.config_history[version2]                 return self._compare_dicts(config1, config2)         def _compare_dicts(self, dict1, dict2, path=""):        """递归比较字典"""        diff = []                 # 检查所有键        all_keys = set(dict1.keys()) | set(dict2.keys())                 for key in all_keys:            new_path = f"{path}.{key}" if path else key                         if key in dict1 and key in dict2:                # 值都是字典则递归比较                if isinstance(dict1[key], dict) and isinstance(dict2[key], dict):                    nested_diff = self._compare_dicts(dict1[key], dict2[key], new_path)                    diff.extend(nested_diff)                # 值相同                elif dict1[key] == dict2[key]:                    diff.append({                        'path': new_path,                        'status': '相同',                        'value': dict1[key]                    })                # 值不同                else:                    diff.append({                        'path': new_path,                        'status': '修改',                        'old_value': dict1[key],                        'new_value': dict2[key]                    })            elif key in dict1:                diff.append({                    'path': new_path,                    'status': '删除',                    'old_value': dict1[key]                })            else:                diff.append({                    'path': new_path,                    'status': '新增',                    'new_value': dict2[key]                })                 return diff         def generate_diff_report(self, version1, version2):        """生成差异报告"""        diff = self.compare_versions(version1, version2)                 report = {            'summary': {                'added': 0,                'deleted': 0,                'modified': 0,                'same': 0            },            'details': []        }                 for item in diff:            report['details'].append(item)            if item['status'] == '新增':                report['summary']['added'] += 1            elif item['status'] == '删除':                report['summary']['deleted'] += 1            elif item['status'] == '修改':                report['summary']['modified'] += 1            else:                report['summary']['same'] += 1                 return report # 使用示例config_system = ConfigDiffAnalyzer() # 添加配置版本config_system.add_config_version('v1.0', {    'database': {'host': 'db1', 'port': 3306},    'logging': {'level': 'INFO'}}) config_system.add_config_version('v1.1', {    'database': {'host': 'db2', 'port': 3306},    'logging': {'level': 'DEBUG'},    'cache': {'enabled': True}}) # 生成差异报告report = config_system.generate_diff_report('v1.0', 'v1.1')print("配置差异报告:")pprint.pprint(report)4.2 数据同步引擎12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576class DataSyncEngine:    """基于字典比较的数据同步引擎"""         def __init__(self):        self.source_data = {}        self.target_data = {}         def load_source(self, data):        """加载源数据"""        self.source_data = data         def load_target(self, data):        """加载目标数据"""        self.target_data = data         def generate_sync_operations(self):        """生成同步操作"""        # 键比较        source_keys = set(self.source_data.keys())        target_keys = set(self.target_data.keys())                 common_keys = source_keys & target_keys        keys_to_add = source_keys - target_keys        keys_to_delete = target_keys - source_keys                 # 生成操作列表        operations = []                 # 添加操作        for key in keys_to_add:            operations.append(('add', key, self.source_data[key]))                 # 删除操作        for key in keys_to_delete:            operations.append(('delete', key))                 # 更新操作        for key in common_keys:            if self.source_data[key] != self.target_data[key]:                operations.append(('update', key, self.source_data[key]))                 return operations         def execute_sync(self):        """执行同步操作"""        operations = self.generate_sync_operations()                 for op in operations:            if op[0] == 'add':                self.target_data[op[1]] = op[2]            elif op[0] == 'delete':                del self.target_data[op[1]]            elif op[0] == 'update':                self.target_data[op[1]] = op[2]                 return self.target_data # 使用示例sync_engine = DataSyncEngine() # 加载数据source = {'a': 1, 'b': 2, 'c': 3}target = {'b': 20, 'c': 3, 'd': 4} sync_engine.load_source(source)sync_engine.load_target(target) # 生成同步操作operations = sync_engine.generate_sync_operations()print("同步操作序列:")for op in operations:    print(op) # 执行同步synced_data = sync_engine.execute_sync()print("同步后数据:", synced_data)4.3 API响应验证系统123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138class APIResponseValidator:    """API响应验证系统"""         def __init__(self):        self.expected_response = None        self.tolerance = 0.01  # 数值容差         def set_expected_response(self, response):        """设置预期响应"""        self.expected_response = response         def validate_response(self, actual_response):        """验证实际响应"""        if self.expected_response is None:            raise ValueError("未设置预期响应")                 return self._compare_dicts(self.expected_response, actual_response)         def _compare_dicts(self, expected, actual, path=""):        """递归比较字典"""        errors = []                 # 检查所有键        expected_keys = set(expected.keys())        actual_keys = set(actual.keys())                 # 缺失键        missing_keys = expected_keys - actual_keys        for key in missing_keys:            errors.append({                'path': f"{path}.{key}" if path else key,                'error': '字段缺失',                'expected': expected[key]            })                 # 多余键        extra_keys = actual_keys - expected_keys        for key in extra_keys:            errors.append({                'path': f"{path}.{key}" if path else key,                'error': '多余字段',                'actual': actual[key]            })                 # 共同键比较        common_keys = expected_keys & actual_keys        for key in common_keys:            new_path = f"{path}.{key}" if path else key            exp_val = expected[key]            act_val = actual[key]                         # 嵌套字典递归比较            if isinstance(exp_val, dict) and isinstance(act_val, dict):                nested_errors = self._compare_dicts(exp_val, act_val, new_path)                errors.extend(nested_errors)            # 列表比较            elif isinstance(exp_val, list) and isinstance(act_val, list):                if len(exp_val) != len(act_val):                    errors.append({                        'path': new_path,                        'error': '列表长度不同',                        'expected': len(exp_val),                        'actual': len(act_val)                    })                else:                    for i, (exp_item, act_item) in enumerate(zip(exp_val, act_val)):                        if isinstance(exp_item, dict) and isinstance(act_item, dict):                            nested_errors = self._compare_dicts(                                exp_item, act_item, f"{new_path}[{i}]"                            )                            errors.extend(nested_errors)                        elif exp_item != act_item:                            errors.append({                                'path': f"{new_path}[{i}]",                                'error': '值不同',                                'expected': exp_item,                                'actual': act_item                            })            # 数值容差比较            elif isinstance(exp_val, (int, float)) and isinstance(act_val, (int, float)):                if abs(exp_val - act_val) > self.tolerance:                    errors.append({                        'path': new_path,                        'error': '数值超出容差',                        'expected': exp_val,                        'actual': act_val,                        'difference': abs(exp_val - act_val)                    })            # 其他类型直接比较            elif exp_val != act_val:                errors.append({                    'path': new_path,                    'error': '值不同',                    'expected': exp_val,                    'actual': act_val                })                 return errors # 使用示例validator = APIResponseValidator() # 设置预期响应expected = {    'status': 'success',    'code': 200,    'data': {        'user': {            'id': 123,            'name': 'Alice',            'scores': [85, 90, 78]        }    }} validator.set_expected_response(expected) # 实际响应actual = {    'status': 'success',    'code': 200,    'data': {        'user': {            'id': 123,            'name': 'Alice Smith',  # 名字不同            'scores': [85, 90.1, 78]  # 数值有微小差异        }    },    'timestamp': '2023-08-15'  # 多余字段} # 验证响应errors = validator.validate_response(actual)print("API响应错误:")for error in errors:    print(f"- [{error['path']}]: {error['error']}")    if 'expected' in error and 'actual' in error:        print(f"  预期: {error['expected']}, 实际: {error['actual']}")五、最佳实践指南5.1 比较策略选择字典比较策略矩阵:┌───────────────────┬──────────────────────────────┬──────────────────────┐│ 场景              │ 需求                         │ 推荐策略              │├───────────────────┼──────────────────────────────┼──────────────────────┤│ 简单键比较        │ 仅需知道共同键               │ 集合交集操作           ││ 精确值匹配        │ 需要完全相同的键值对         │ items()集合交集        ││ 内容差异分析      │ 需要详细差异报告             │ 递归比较算法           ││ 大型字典比较      │ 高性能需求                   │ 并行比较+惰性计算      ││ 模糊匹配          │ 类型/格式/大小写差异         │ 自定义比较函数         ││ 实时同步          │ 最小化传输数据               │ 键/值差异同步策略      │└───────────────────┴──────────────────────────────┴──────────────────────┘5.2 性能优化检查表​​预处理优化​​:对大型字典预先计算键集合对嵌套字典进行扁平化处理对不可变字典使用哈希加速​​比较过程优化​​:使用惰性计算避免不必要比较对大型字典采用分块比较使用多线程/多进程并行比较​​内存优化​​:使用生成器避免创建大型中间数据结构对字符串值使用intern减少内存对数值使用数组存储5.3 错误处理策略1234567891011121314151617181920212223242526272829303132333435363738def safe_dict_compare(dict1, dict2):    """带错误处理的字典比较"""    try:        # 键比较        common_keys = set(dict1.keys()) & set(dict2.keys())                 # 值比较        same_items = {}        diff_items = {}                 for key in common_keys:            try:                if dict1[key] == dict2[key]:                    same_items[key] = dict1[key]                else:                    diff_items[key] = (dict1[key], dict2[key])            except Exception as e:                # 记录比较错误                diff_items[key] = f"比较错误: {str(e)}"                 return {            'same_items': same_items,            'diff_items': diff_items,            'unique_to_dict1': set(dict1.keys()) - common_keys,            'unique_to_dict2': set(dict2.keys()) - common_keys        }    except Exception as e:        # 返回错误信息        return {            'error': f"字典比较失败: {str(e)}",            'exception_type': type(e).__name__        } # 使用示例dict_with_error = {'a': 1, 'b': object()}  # 不可比较对象result = safe_dict_compare({'a': 1, 'b': 2}, {'a': 1, 'b': dict_with_error})print("安全比较结果:")pprint.pprint(result)总结:字典比较技术全景通过本文的全面探讨,我们掌握了两个字典相同点查找的:​​基础方法​​:键、值、键值对比较​​高级技术​​:嵌套字典、自定义比较​​性能优化​​:大型字典处理、并行计算​​企业应用​​:配置管理、数据同步、API验证​​错误处理​​:健壮性设计​​最佳实践​​:场景化策略选择字典比较黄金法则:1. 明确需求:选择合适比较粒度2. 优先简单:使用集合操作处理基础需求3. 递归处理:嵌套结构使用递归算法4. 性能敏感:大型数据采用优化策略5. 容错设计:考虑边界情况和异常性能对比数据字典比较方法性能对比(百万键值对):┌───────────────────┬───────────────┬───────────────┬──────────────┐│ 方法              │ 耗时(秒)      │ 内存占用(MB)  │ 适用场景      │├───────────────────┼───────────────┼───────────────┼──────────────┤│ 基础集合操作       │ 0.8           │ 120           │ 简单键比较    ││ 完整键值对比较     │ 2.5           │ 350           │ 精确匹配      ││ 递归比较          │ 5.2           │ 520           │ 嵌套结构      ││ 并行比较(8核)     │ 0.9           │ 180           │ 大型字典      ││ 惰性比较          │ 1.2           │ 95            │ 部分结果需求  │└───────────────────┴───────────────┴───────────────┴──────────────┘技术演进方向​​增量比较​​:仅比较变更部分​​AI辅助比较​​:智能识别语义相似性​​分布式比较​​:集群环境大规模数据比较​​二进制比较​​:内存级高效比较​​版本化比较​​:支持多版本差异分析
  • [技术干货] Python实现固定列宽文本格式化终极指南【转载】
    一、基础格式化:textwrap模块1.1 基础文本换行1234567891011121314151617import textwrap text = "Python is an interpreted, high-level, general-purpose programming language." # 基础换行wrapped = textwrap.fill(text, width=30)print(wrapped)"""Python is an interpreted,high-level, general-purposeprogramming language.""" # 保留段落结构paragraphs = text.split('. ')wrapped_paragraphs = [textwrap.fill(p, width=30) for p in paragraphs]print("\n\n".join(wrapped_paragraphs)) 1.2 高级格式控制1234567891011121314151617181920212223242526272829# 自定义首行缩进formatted = textwrap.fill(    text,    width=40,    initial_indent='> ',    # 首行缩进    subsequent_indent='| '   # 后续行缩进)print(formatted)"""> Python is an interpreted, high-| level, general-purpose| programming language.""" # 处理长单词long_word_text = "This sentence contains a verylongwordthatneedstobebroken."wrapped = textwrap.fill(    long_word_text,    width=20,    break_long_words=True,    break_on_hyphens=False)print(wrapped)"""This sentencecontains averylongwordthatneedstobebroken.""" 二、进阶技术:自定义格式化器2.1 固定列宽格式化类12345678910111213141516171819202122232425262728293031323334353637383940414243class ColumnFormatter:    """高级列宽格式化器"""    def __init__(self, width=80, indent=0, hanging_indent=0):        self.width = width        self.indent = indent        self.hanging_indent = hanging_indent         def format(self, text):        """格式化文本"""        # 移除多余空白        text = ' '.join(text.split())                 # 初始化结果列表        lines = []        current_line = []        current_length = self.indent                 # 处理每个单词        for word in text.split():            word_length = len(word)                         # 检查是否超出宽度            if current_length + word_length + len(current_line) > self.width:                # 添加当前行                lines.append(" " * self.indent + " ".join(current_line))                                 # 重置行                current_line = [word]                current_length = self.hanging_indent + word_length            else:                current_line.append(word)                current_length += word_length                 # 添加最后一行        if current_line:            lines.append(" " * self.indent + " ".join(current_line))                 return "\n".join(lines) # 使用示例formatter = ColumnFormatter(width=30, indent=4, hanging_indent=2)formatted_text = formatter.format(text)print(formatted_text)2.2 两端对齐算法1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859def justify_text(text, width):    """两端对齐文本"""    words = text.split()    lines = []    current_line = []    current_length = 0         for word in words:        # 计算添加单词后的长度        if current_line:            new_length = current_length + len(word) + 1        else:            new_length = len(word)                 if new_length <= width:            current_line.append(word)            current_length = new_length        else:            # 对当前行进行两端对齐            lines.append(justify_line(current_line, width))            current_line = [word]            current_length = len(word)         # 处理最后一行(左对齐)    if current_line:        lines.append(" ".join(current_line))         return "\n".join(lines) def justify_line(words, width):    """单行两端对齐"""    if len(words) == 1:        return words[0].ljust(width)         # 计算总空格数    total_chars = sum(len(word) for word in words)    total_spaces = width - total_chars    gaps = len(words) - 1         # 计算基本空格和额外空格    base_spaces = total_spaces // gaps    extra_spaces = total_spaces % gaps         # 构建行    line = words[0]    for i in range(1, len(words)):        # 分配空格        spaces = base_spaces + (1 if i <= extra_spaces else 0)        line += ' ' * spaces + words[i]         return line # 测试text = "Python is an interpreted high-level general-purpose programming language."print(justify_text(text, 40))"""Python   is  an  interpreted  high-levelgeneral-purpose programming language.""" 三、表格数据格式化 3.1 基础表格生成123456789101112131415161718192021222324252627282930def generate_table(data, headers, col_width=15):    """生成固定列宽表格"""    # 表头分隔线    separator = '+' + '+'.join(['-' * col_width] * len(headers)) + '+'         # 构建表头    header_line = '|' + '|'.join(        f"{h:^{col_width}}" for h in headers    ) + '|'         # 构建数据行    rows = []    for row in data:        row_line = '|' + '|'.join(            f"{str(cell):^{col_width}}" for cell in row        ) + '|'        rows.append(row_line)         # 组合表格    return '\n'.join([separator, header_line, separator] + rows + [separator]) # 使用示例headers = ["Product", "Price", "Stock"]data = [    ["Laptop", 999.99, 15],    ["Phone", 699.99, 30],    ["Tablet", 399.99, 25]] print(generate_table(data, headers)) 3.2 自适应列宽表格123456789101112131415161718192021222324252627282930313233343536373839404142434445464748class SmartTableFormatter:    """智能表格格式化器"""    def __init__(self, headers, alignments=None):        self.headers = headers        self.alignments = alignments or ['^'] * len(headers)        self.col_widths = [len(h) for h in headers]         def update_widths(self, row):        for i, cell in enumerate(row):            cell_str = str(cell)            self.col_widths[i] = max(self.col_widths[i], len(cell_str))         def format_row(self, row):        formatted = []        for i, cell in enumerate(row):            width = self.col_widths[i]            align = self.alignments[i]            formatted.append(f"{str(cell):{align}{width}}")        return '| ' + ' | '.join(formatted) + ' |'         def format_table(self, data):        # 计算列宽        for row in data:            self.update_widths(row)                 # 生成分隔线        separator = '+-' + '-+-'.join(            '-' * (w + 2) for w in self.col_widths        ) + '-+'                 # 生成表头        header_row = self.format_row(self.headers)                 # 生成数据行        data_rows = [self.format_row(row) for row in data]                 return '\n'.join([separator, header_row, separator] + data_rows + [separator]) # 使用示例headers = ["Product", "Price", "Stock", "Rating"]data = [    ["Laptop", 999.99, 15, 4.5],    ["Smartphone", 699.99, 30, 4.7],    ["Tablet", 399.99, 25, 4.3]] formatter = SmartTableFormatter(headers, alignments=['<', '>', '>', '^'])print(formatter.format_table(data)) 四、终端输出美化 4.1 终端宽度自适应12345678910111213141516171819202122import osimport shutil def adaptive_wrap(text, indent=0):    """根据终端宽度自适应换行"""    # 获取终端宽度    terminal_width = shutil.get_terminal_size().columns         # 计算可用宽度    available_width = terminal_width - indent         # 格式化文本    return textwrap.fill(        text,        width=available_width,        initial_indent=' ' * indent,        subsequent_indent=' ' * indent    ) # 使用示例long_text = "Python is an interpreted, high-level, general-purpose programming language."print(adaptive_wrap(long_text, indent=4)) 4.2 彩色表格输出123456789101112131415161718192021222324252627282930313233343536373839404142def colorize_table(data, headers, col_widths):    """带颜色的表格输出"""    # ANSI颜色代码    COLORS = {        'header': '\033[1;34m',  # 加粗蓝色        'row': '\033[0m',        # 默认        'alt_row': '\033[38;5;245m', # 灰色        'reset': '\033[0m'    }         # 表头分隔线    separator = '+' + '+'.join(['-' * w for w in col_widths]) + '+'         # 构建表头    header_line = '|' + '|'.join(        f"{COLORS['header']}{h:^{w}}{COLORS['reset']}"        for h, w in zip(headers, col_widths)    ) + '|'         # 构建数据行    rows = []    for i, row in enumerate(data):        color = COLORS['alt_row'] if i % 2 == 1 else COLORS['row']        row_line = '|' + '|'.join(            f"{color}{str(cell):^{w}}{COLORS['reset']}"            for cell, w in zip(row, col_widths)        ) + '|'        rows.append(row_line)         # 组合表格    return '\n'.join([separator, header_line, separator] + rows + [separator]) # 使用示例headers = ["ID", "Name", "Score"]data = [    [1, "Alice", 95.5],    [2, "Bob", 88.2],    [3, "Charlie", 91.8]]col_widths = [5, 10, 8] print(colorize_table(data, headers, col_widths)) 五、日志文件格式化 5.1 日志消息对齐123456789101112131415161718192021222324252627282930313233343536373839404142class FixedWidthLogFormatter(logging.Formatter):    """固定宽度日志格式化器"""    def __init__(self, widths, fmt=None, datefmt=None):        """        widths: 各字段宽度字典        """        self.widths = widths        super().__init__(fmt, datefmt)         def format(self, record):        # 格式化时间        record.asctime = self.formatTime(record, self.datefmt)                 # 对齐各字段        for field, width in self.widths.items():            if hasattr(record, field):                value = getattr(record, field)                setattr(record, field, f"{value:<{width}}")                 return super().format(record) # 配置日志logger = logging.getLogger("app")handler = logging.StreamHandler() formatter = FixedWidthLogFormatter(    widths={        'levelname': 8,        'name': 15,        'funcName': 15    },    fmt='%(asctime)s | %(levelname)s | %(name)s | %(funcName)s | %(message)s',    datefmt='%Y-%m-%d %H:%M:%S') handler.setFormatter(formatter)logger.addHandler(handler)logger.setLevel(logging.DEBUG) # 测试日志logger.debug("Starting application")logger.info("Configuration loaded") 5.2 日志文件列对齐1234567891011121314151617181920212223def align_log_file(input_path, output_path, column_widths):    """对齐日志文件列"""    with open(input_path, 'r') as fin:        with open(output_path, 'w') as fout:            for line in fin:                # 分割列                columns = line.strip().split('|')                                 # 对齐每列                aligned = []                for i, col in enumerate(columns):                    width = column_widths[i] if i < len(column_widths) else 20                    aligned.append(f"{col.strip():<{width}}")                                 # 写入对齐后的行                fout.write(" | ".join(aligned) + "\n") # 使用示例align_log_file(    "raw.log",     "aligned.log",    column_widths=[25, 10, 15, 40]) 六、性能优化:大文件处理 6.1 流式格式化处理1234567891011121314151617181920def stream_text_format(input_file, output_file, width=80):    """大文件流式格式化"""    with open(input_file, 'r') as fin:        with open(output_file, 'w') as fout:            buffer = ""            while True:                chunk = fin.read(4096)                if not chunk and not buffer:                    break                                 buffer += chunk                while '\n' in buffer:                    line, buffer = buffer.split('\n', 1)                    wrapped = textwrap.fill(line, width=width)                    fout.write(wrapped + '\n')                         # 处理剩余内容            if buffer:                wrapped = textwrap.fill(buffer, width=width)                fout.write(wrapped) 6.2 内存高效表格生成1234567891011121314151617181920212223242526272829303132333435363738def large_table_generator(data_source, headers, col_widths):    """生成大型表格的生成器"""    # 计算分隔线    separator = '+' + '+'.join(['-' * w for w in col_widths]) + '+'         # 生成表头    header_line = '|' + '|'.join(        f"{h:^{w}}" for h, w in zip(headers, col_widths)    ) + '|'         # 返回生成器    yield separator    yield header_line    yield separator         # 生成数据行    for row in data_source:        row_line = '|' + '|'.join(            f"{str(cell):^{w}}" for cell, w in zip(row, col_widths)        ) + '|'        yield row_line         yield separator # 使用示例def mock_data_generator(count):    """模拟大数据生成器"""    for i in range(count):        yield [f"Product{i}", i * 10, i % 5 + 1] # 处理100万行数据with open("big_table.txt", "w") as f:    for line in large_table_generator(        mock_data_generator(1000000),        headers=["Name", "Price", "Stock"],        col_widths=[20, 10, 10]    ):        f.write(line + "\n") 七、实战案例:日志分析报告 7.1 日志分析报告生成123456789101112131415161718192021222324252627282930313233343536373839404142434445def generate_log_report(log_file, output_file, width=80):    """生成固定列宽的日志分析报告"""    # 分析日志数据    error_count = 0    warning_count = 0    log_entries = []         with open(log_file, 'r') as f:        for line in f:            if "ERROR" in line:                error_count += 1                log_entries.append(("ERROR", line.strip()))            elif "WARNING" in line:                warning_count += 1                log_entries.append(("WARNING", line.strip()))         # 生成报告    report = [        "=" * width,        f"Log Analysis Report".center(width),        "=" * width,        "",        f"Total Errors: {error_count}".ljust(width),        f"Total Warnings: {warning_count}".ljust(width),        "",        "Detailed Log Entries:",        "-" * width    ]         # 添加日志条目    for level, entry in log_entries:        wrapped_entry = textwrap.fill(            f"[{level}] {entry}",            width=width,            initial_indent='  ',            subsequent_indent='    '        )        report.append(wrapped_entry)         # 写入文件    with open(output_file, 'w') as f:        f.write("\n".join(report)) # 使用示例generate_log_report("app.log", "log_report.txt", width=100) 7.2 数据库查询结果格式化1234567891011121314151617181920212223242526272829303132def format_query_results(connection, query, output_width=80):    """格式化数据库查询结果"""    cursor = connection.cursor()    cursor.execute(query)         # 获取列名    columns = [desc[0] for desc in cursor.description]    col_width = output_width // len(columns)         # 生成表头    header = "|".join(f"{col:^{col_width}}" for col in columns)    separator = "-" * output_width         # 构建报告    report = [separator, header, separator]         # 添加数据行    for row in cursor.fetchall():        row_str = "|".join(            f"{str(value)[:col_width-3]:^{col_width}}"            for value in row        )        report.append(row_str)         report.append(separator)    return "\n".join(report) # 使用示例import sqlite3conn = sqlite3.connect("example.db")query = "SELECT id, name, price FROM products"print(format_query_results(conn, query, output_width=100)) 八、最佳实践与性能优化 8.1 格式化方法性能对比12345678910111213141516171819202122232425262728293031import timeit # 测试数据text = "Python is an interpreted, high-level, general-purpose programming language." * 100 # 测试函数def test_textwrap():    return textwrap.fill(text, width=80) def test_custom_formatter():    formatter = ColumnFormatter(width=80)    return formatter.format(text) def test_justify():    return justify_text(text, width=80) # 性能测试methods = {    "textwrap": test_textwrap,    "custom_formatter": test_custom_formatter,    "justify": test_justify} results = {}for name, func in methods.items():    time = timeit.timeit(func, number=100)    results[name] = time print("100次操作耗时:")for name, time in sorted(results.items(), key=lambda x: x[1]):    print(f"{name}: {time:.4f}秒") 8.2 固定列宽格式化决策树8.3 黄金实践原则​​选择合适工具​​:简单文本:textwrap表格数据:自定义表格格式化器终端输出:自适应宽度​​性能优化策略​​:1234# 大文件使用流式处理with open("large.txt") as fin, open("formatted.txt", "w") as fout:    for line in fin:        fout.write(textwrap.fill(line, width=80) + "\n")​​终端美化技巧​​:12# 添加颜色增强可读性print(f"\033[1;32m{header.center(width)}\033[0m")​​国际化考虑​​:1234# 处理全角字符from wcwidth import wcswidthdef real_width(text):    return wcswidth(text)​​错误处理机制​​:12345try:    formatted = justify_text(text, width)except ValueError as e:    # 回退到简单格式化    formatted = textwrap.fill(text, width)​​单元测试覆盖​​:123456789101112class TestTextFormatting(unittest.TestCase):    def test_textwrap_formatting(self):        text = "This is a test."        result = textwrap.fill(text, width=10)        self.assertEqual(result, "This is a\ntest.")         def test_table_formatting(self):        headers = ["Name", "Age"]        data = [["Alice", 30]]        table = generate_table(data, headers)        self.assertIn("Alice", table)        self.assertIn("30", table) 总结:固定列宽格式化技术全景9.1 技术选型矩阵场景推荐方案优势复杂度​​简单文本换行​​textwrap简单易用★☆☆☆☆​​高级文本格式化​​ColumnFormatter灵活控制★★☆☆☆​​表格数据展示​​SmartTableFormatter专业美观★★★☆☆​​终端输出美化​​自适应宽度+颜色交互友好★★★☆☆​​日志文件处理​​FixedWidthLogFormatter结构清晰★★☆☆☆​​大文件处理​​流式处理内存高效★★★★☆​​数据库结果格式化​​查询结果格式化器数据驱动★★★☆☆ 9.2 核心原则总结​​理解需求场景​​:报表生成:表格格式化器日志处理:固定宽度日志格式化器终端输出:自适应宽度​​性能优化优先​​:大文件使用流式处理避免不必要的中间字符串预编译格式化器​​可读性至上​​:合理使用对齐方式添加适当空白和分隔符使用颜色增强可读性​​错误处理机制​​:处理超长单词处理混合编码提供回退方案​​国际化支持​​:处理全角字符宽度支持多语言文本考虑文本方向​​测试驱动开发​​:单元测试所有格式化器边界条件测试性能基准测试
  • [技术干货] Python从Word文档中提取图片并生成PPT的操作代码【转载】
    背景与需求假设你有一个文件夹,里面包含多个 .docx 格式的 Word 文档,每个文档中嵌入了若干图片。你的目标是:提取图片:从每个 Word 文档中提取所有嵌入的图片。生成 PPT:为每张图片创建一个独立的幻灯片,并将其插入到 PowerPoint 文件中。批量处理:能够一次性处理文件夹中的所有 Word 文档。通过 Python 的强大功能,我们可以轻松实现这一流程。解决方案概述我们将使用以下 Python 库来完成任务:python-docx:用于读取 Word 文档内容。python-pptx:用于创建 PowerPoint 幻灯片。Pillow:用于处理图片尺寸和格式。io:用于处理内存中的二进制数据流。整个流程分为以下几个步骤:遍历指定文件夹中的所有 Word 文件。使用 python-docx 提取 Word 文档中的图片。将提取的图片插入到 PowerPoint 幻灯片中。保存生成的 PPT 文件。代码解析以下是完整的 Python 代码及其详细说明:1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859from docx import Documentfrom pptx import Presentationfrom pptx.util import Inchesfrom PIL import Imageimport ioimport os # 指定包含 Word 文件的文件夹路径folder_path = r"C:\Users\Administrator\Desktop\tt"  # 修改为你的文件夹路径 # 遍历文件夹中的所有 Word 文件for filename in os.listdir(folder_path):    if filename.endswith(".docx"):        word_path = os.path.join(folder_path, filename)        ppt_name = os.path.splitext(filename)[0] + ".pptx"        ppt_path = os.path.join(folder_path, ppt_name)         print(f"正在处理:{filename}")         try:            # 尝试读取Word文件            doc = Document(word_path)         except Exception as e:            print(f"❌ 无法读取文件 {filename}: {e}")            continue  # 跳过当前文件,处理下一个         # 创建PPT演示文稿        prs = Presentation()         # 提取图片并插入PPT        for para in doc.paragraphs:            for run in para.runs:                blip = run._r.find('.//a:blip', namespaces={'a': 'http://schemas.openxmlformats.org/drawingml/2006/main'})                if blip is not None:                    embed_id = blip.get('{http://schemas.openxmlformats.org/officeDocument/2006/relationships}embed')                    if embed_id and embed_id in run.part.related_parts:                        image_part = run.part.related_parts[embed_id]                        img_stream = io.BytesIO(image_part.blob)                         # 打开图片                        img = Image.open(img_stream)                        img_width, img_height = img.size                         # 创建PPT幻灯片                        slide = prs.slides.add_slide(prs.slide_layouts[6])                         # 设置PPT大小与图片尺寸一致                        prs.slide_width = Inches(img_width / 100)                        prs.slide_height = Inches(img_height / 100)                         # 将图片添加到幻灯片                        slide.shapes.add_picture(img_stream, 0, 0, width=Inches(img_width / 100), height=Inches(img_height / 100))         # 保存PPT        prs.save(ppt_path)        print(f"✅ 生成PPT:{ppt_path}") print("所有文件处理完成!🎯")代码核心逻辑说明遍历文件夹:使用 os.listdir() 遍历指定文件夹中的所有文件,并筛选出 .docx 格式的文件。读取 Word 文件:使用 python-docx 的 Document 类加载 Word 文档。如果文件损坏或无法读取,捕获异常并跳过该文件。提取图片:遍历 Word 文档中的段落 (paragraphs) 和运行块 (runs)。使用 XML 命名空间查找嵌入的图片资源 (blip),并通过 embed_id 获取图片的二进制数据。创建 PPT 幻灯片:使用 python-pptx 创建一个新的 PowerPoint 演示文稿。为每张图片创建一个空白幻灯片,并调整幻灯片尺寸以适应图片。保存 PPT 文件:将生成的 PPT 文件保存到指定路径,并打印成功信息。
  • [技术干货] 【专题汇总】 八月份技术干货汇总它虽迟但到,快进来瞧瞧吧!
    大家好,8月份的技术汇总贴它虽迟但到。本次带来了关于SpringBoot,C++,Java,python,GO语言等等多方面知识,量大管饱,希望可以帮助到大家。1、Java报错:org.springframework.beans.factory.BeanCreationException的五种解决方法【转载】cid:link_02、C#控制台程序同步调用WebApi实现方式【转载】cid:link_73、java -jar example.jar 产生的日志输出到指定文件的方法【转载】cid:link_84、 SpringBoot项目自定义静态资源映射规则的实现代码【转载】cid:link_15、 SpringBoot中9个内置过滤器用法的完整指南【转载】cid:link_26、C++中的list与forward_list介绍与使用【转载】cid:link_37、Java使用Redis实现消息订阅/发布的几种方式【转载】cid:link_98、Spring Boot中使用@Scheduled和Quartz实现定时任务的详细过程【转载】cid:link_109、 Python中的sort()和sorted()用法示例解析【转载】https://bbs.huaweicloud.com/forum/thread-02102190373187063025-1-1.html10、 python中update()函数的用法和一些例子【转载】cid:link_411、基于Python编写新手向的简易翻译工具 【转载】cid:link_1112、使用Python创建PowerPoint各种图表的详细教程【转载】cid:link_1213、python basicConfig()简介及用法举例【转载】cid:link_514、 Python 存根文件(.pyi)简介与实战案例及类型提示的高级指南【转载】cid:link_615、Go轻松构建WebSocket服务器的实现方案【转载】https://bbs.huaweicloud.com/forum/thread-0294190373041865026-1-1.html
  • [技术干货] python字符串删除空行
    在Python中,删除字符串中的空行可以通过多种方法实现。以下是几种常见的方法:方法1:使用 splitlines() 和列表推导式text = """这是第一行 这是第二行 这是第三行""" # 删除空行 lines = [line for line in text.splitlines() if line.strip() != ""] result = "\n".join(lines) print(result) 方法2:使用 split() 和 join()text = """这是第一行 这是第二行 这是第三行""" # 删除空行 lines = text.split("\n") non_empty_lines = [line for line in lines if line.strip() != ""] result = "\n".join(non_empty_lines) print(result) 方法3:使用正则表达式import re text = """这是第一行 这是第二行 这是第三行""" # 删除空行(包括只有空白字符的行) result = re.sub(r'\n\s*\n', '\n', text).strip() print(result) 方法4:逐行处理(适用于大文件)text = """这是第一行 这是第二行 这是第三行""" # 删除空行 result = "" for line in text.splitlines(): if line.strip(): result += line + "\n" result = result.strip() # 移除末尾可能的空行 print(result) 输出结果以上所有方法的输出均为:这是第一行 这是第二行 这是第三行关键点说明strip():用于检查行是否为空(或仅包含空白字符)。splitlines() vs split("\n"):splitlines() 会正确处理不同平台的换行符(\n, \r, \r\n)。split("\n") 是显式按 \n 分割。正则表达式:可以一次性替换多个连续空行,但可能对复杂情况需调整。处理文件中的空行如果是从文件读取并删除空行,可以这样操作:with open("input.txt", "r", encoding="utf-8") as f: lines = [line.strip() for line in f if line.strip()] with open("output.txt", "w", encoding="utf-8") as f: f.write("\n".join(lines)) 选择方法时,根据数据量大小和具体需求(如是否保留缩进)决定即可。
总条数:2474 到第 页
上滑加载中