【python】python文件=注释=书写建议,基于pycharm

一个方法的备注,书写的好;

方便他人查看,方法的作用;

方便不进入方法就能查看,光标移动到方法上就行

方便查看传参,返回值

显得有 逼格吧!,规范化!

  • 建议如下格式书写,方便阅读查看~
python 复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2025-02-14 14:31
# @Author  : duxiaowei
# @File    : report_css_into_html.py
# @Software: 将报告中的css文件插入到html文件中,方便jira下载查看
import os


def rewrite_html(html_file_path):
    """将报告中的css文件插入到html文件中,方便上传jira,下载查看
        Args:
            html_file_path: 文件路径
        Returns:
            无
        Raises:
            写入失败
    """
    path_html = os.path.abspath('..') + '\\test_API\\reports\\' + html_file_path
    path_css = os.path.abspath('..') + '\\test_API\\reports\\assets\\style.css'
    try:
        # 读取 HTML 文件内容
        with open(path_html, 'r', encoding='GB2312') as html_file:
            html_content = html_file.read()

        # 读取 CSS 文件内容
        with open(path_css, 'r', encoding='GB2312') as path_css:
            css_content = path_css.read()

        # 定义要查找的 <link> 标签
        link_tag = '<link href="assets/style.css" rel="stylesheet" type="text/css"/>'

        # 生成新的 <style> 标签内容
        style_tag = f'<style>\n{css_content}\n</style>'

        # 替换 <link> 标签为 <style> 标签
        new_html_content = html_content.replace(link_tag, style_tag)

        # 将修改后的内容写回 HTML 文件
        with open(path_html, 'w', encoding='GB2312') as html_file:
            html_file.write(new_html_content)

        # print("CSS 文件内容已成功插入到 HTML 文件中。")
    except FileNotFoundError:
        print("未找到 HTML 或 CSS 文件,请检查文件路径。")
    except Exception as e:
        print(f"处理文件时出现错误: {e}")
相关推荐
xchenhao1 小时前
SciKit-Learn 全面分析分类任务 breast_cancer 数据集
python·机器学习·分类·数据集·scikit-learn·svm
独行soc4 小时前
2025年渗透测试面试题总结-66(题目+回答)
java·网络·python·安全·web安全·adb·渗透测试
Y学院6 小时前
Python 数据分析:从新手到高手的“摸鱼”指南
python·数据分析
深耕AI7 小时前
【PyTorch训练】准确率计算(代码片段拆解)
人工智能·pytorch·python
eqwaak07 小时前
科技信息差(9.12)
开发语言·python·科技·量子计算
Blossom.1187 小时前
从“能写”到“能干活”:大模型工具调用(Function-Calling)的工程化落地指南
数据库·人工智能·python·深度学习·机器学习·计算机视觉·oracle
蒋星熠8 小时前
破壁者指南:内网穿透技术的深度解构与实战方法
网络·数据库·redis·python·websocket·网络协议·udp
shizidushu8 小时前
使用 Pyinstaller 打包 PPOCRLabel
python·pyinstaller
Q_Q19632884758 小时前
python+springboot+uniapp微信小程序题库系统 在线答题 题目分类 错题本管理 学习记录查询系统
spring boot·python·django·uni-app·node.js·php