python 修改cnblogs-blogger-downloader下载的文件为md格式

python 修改cnblogs-blogger-downloader下载的文件为md格式

目录格式: mian.py文件 和blog-dir目录 在同级目录, blog-dir为 cnblogs-blogger-downloader下载的文件分类 各分类里是随笔文件

编辑 vim mian.py

import os
import re

# 函数 对相对文件路径的文件进行 变更修改 并保存
def change_file(filepath):
    # 打开文件并读取内容
    with open(filepath, 'r', encoding='utf-8') as file:
        lines = file.readlines()
    # 检查文件是否有至少两行内容
    if len(lines) >= 2:
        # 读取第二行内容
        second_line = lines[1]
        # 查找冒号的位置
        colon_pos = second_line.find(':')
        if colon_pos != -1:
            # 提取冒号后面的字符串并去除空格
            result = second_line[colon_pos + 1:].strip()
            # 删除前7行
            lines = lines[7:]
            # 将字符串加上前缀# 插入到第一行
            lines.insert(0, f'# {result}\n')
        # 处理代码块标识# 替换 csharp 代码块为 python 代码块
        new_lines = [line.replace('```csharp', '```') for line in lines]
        # 定义代码框字样 "```"
        inside_code_block = False
        new_code_lines = []
        code_block_pattern = re.compile(r'```')
        # 取列表行号和元素
        for i, line in enumerate(new_lines):
            # 去掉前后空格
            stripped_line = line.strip()
            # 判断是代码框包裹吗
            if code_block_pattern.match(stripped_line.strip()):
                inside_code_block = not inside_code_block
                new_code_lines.append(line) # 是则保持代码框行内不变
            else: 
                # 判断没在代码框,并且stripped_line不为空
                if not inside_code_block and stripped_line:
                    # 如果是第一行,不添加前缀
                    if i == 0:
                        new_code_lines.append(f"{line.strip()}\n")
                    # 如果不是第一行,则添加"### "前缀
                    else:
                        new_code_lines.append(f"### {stripped_line}\n")
                # 判断在代码框内,则直接加入
                else:
                    new_code_lines.append(line)
        # 写回文件
        with open(filepath, 'w', encoding='utf-8') as file:
            file.writelines(new_code_lines)

# 以mian.py为根 循环目录 返回得到 文件的相对路径列表(排除 主执行文件 mian.py)
def list_files_in_subdirs(root_dir, exclude_file="mian.py"):
    files_in_subdirs = []
    digit_pattern = re.compile(r'^\d')
    for dirpath, dirnames, filenames in os.walk(root_dir):
        # 检查当前目录是否包含文件
        if filenames:
            for filename in filenames:
                if filename != exclude_file and not digit_pattern.match(filename):
                    relative_path = os.path.relpath(os.path.join(dirpath, filename), root_dir)
                    files_in_subdirs.append(relative_path)
    return files_in_subdirs
 
# 设置目标目录
root_dir = '.'
# 调用 list_files_in_subdirss 函数 返回得到文件的相对路径的列表
list_files_relative_path = list_files_in_subdirs(root_dir)
for filepath in list_files_relative_path:
    change_file(filepath)

执行python脚本

python.exe mian.py

posted on   luokeli  阅读(2)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示