You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Batch脚本解析Markdown生成requirements.txt

解决方案:Batch脚本提取Markdown指定区块内容

以下是能精准实现需求的Batch脚本,可直接使用:

@echo off
setlocal enabledelayedexpansion

:: 配置文件路径,替换为你的实际文件
set "md_file=input.md"
set "output_file=requirements.txt"

:: 清空输出文件,避免残留旧内容
type nul > "%output_file%"

set "in_target_section=0"

:: 逐行读取Markdown文件
for /f "delims=" %%a in ('type "%md_file%"') do (
    set "current_line=%%a"
    
    :: 检测进入目标区块:匹配"## Python Packages"
    if "!current_line:~0,18!"=="## Python Packages" (
        set "in_target_section=1"
        goto next_line
    )
    
    :: 处于目标区块内时处理内容
    if !in_target_section! equ 1 (
        :: 检测离开区块:遇到下一个以##开头的标题
        if "!current_line:~0,2!"=="##" (
            set "in_target_section=0"
            goto next_line
        )
        
        :: 处理行:移除开头的连字符及后续空格
        set "processed_line=!current_line!"
        :trim_start
        :: 循环移除开头的连字符
        if "!processed_line:~0,1!"=="-" (
            set "processed_line=!processed_line:~1!"
            goto trim_start
        )
        :: 循环移除连字符后残留的空格
        if "!processed_line:~0,1!"==" " (
            set "processed_line=!processed_line:~1!"
            goto trim_start
        )
        
        :: 非空行才写入输出文件(可根据需求删除此判断保留空行)
        if not "!processed_line!"=="" (
            echo !processed_line!>>"%output_file%"
        )
    )
    
    :next_line
)

echo 处理完成,结果已写入%output_file%
endlocal

关键逻辑说明

  1. 区块标记控制:用in_target_section变量标记是否处于## Python Packages区块内,找到目标标题时激活标记,遇到下一个##标题时关闭标记。
  2. 精准字符串匹配:通过截取行首字符判断目标标题和区块结束点,避免误匹配。
  3. 灵活的前缀清理:通过循环移除开头的所有连字符及后续空格,兼容- package、-package、- package等多种格式。
  4. 延迟变量扩展:启用enabledelayedexpansion确保循环内变量能实时更新,避免输出混乱。

使用方法

  1. 将脚本保存为extract_requirements.bat。
  2. 修改md_file变量为你的Markdown文件路径(如set "md_file=docs.md")。
  3. 双击运行脚本,处理后的内容会写入当前目录的requirements.txt。

内容的提问来源于stack exchange,提问作者futium

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 11:47:33