批量去除文本中标记的脚本

wen 实用脚本 2

本文目录导读:

批量去除文本中标记的脚本

  1. 去除HTML/XML标签(Python)
  2. 去除Markdown标记(Python)
  3. 去除自定义标记(如{...}、[...]等)
  4. 使用Sed命令(Linux/Mac)
  5. 使用PowerShell(Windows)
  6. 使用说明

去除HTML/XML标签(Python)

import re
def remove_tags(text):
    # 去除所有HTML/XML标签
    clean = re.compile('<.*?>')
    return re.sub(clean, '', text)
# 对文件批量处理
def process_file(input_file, output_file):
    with open(input_file, 'r', encoding='utf-8') as f:
        content = f.read()
    cleaned_content = remove_tags(content)
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write(cleaned_content)
# 批量处理多个文件
import os
input_folder = 'input/'
output_folder = 'output/'
for filename in os.listdir(input_folder):
    if filename.endswith('.txt'):
        input_path = os.path.join(input_folder, filename)
        output_path = os.path.join(output_folder, f'cleaned_{filename}')
        process_file(input_path, output_path)

去除Markdown标记(Python)

import re
def remove_markdown(text):
    # 去除标题标记
    text = re.sub(r'^#{1,6}\s+', '', text, flags=re.MULTILINE)
    # 去除粗体和斜体
    text = re.sub(r'\*\*(.*?)\*\*', r'\1', text)
    text = re.sub(r'\*(.*?)\*', r'\1', text)
    # 去除链接
    text = re.sub(r'\[([^\]]*)\]\([^)]*\)', r'\1', text)
    # 去除图片
    text = re.sub(r'!\[([^\]]*)\]\([^)]*\)', r'\1', text)
    # 去除引用标记
    text = re.sub(r'^>\s+', '', text, flags=re.MULTILINE)
    # 去除代码块标记
    text = re.sub(r'```[\s\S]*?```', '', text)
    text = re.sub(r'`([^`]*)`', r'\1', text)
    return text
# 使用示例
md_text = "# 标题\n这是**加粗**和*斜体*文本"
print(remove_markdown(md_text))

去除自定义标记(如{...}、[...]等)

import re
def remove_custom_tags(text, tag_pattern=None):
    """
    去除自定义标记
    tag_pattern: 正则表达式,默认去除尖括号、方括号、花括号内容
    """
    if tag_pattern is None:
        # 默认去除常见括号标记
        tag_patterns = [
            r'\{[^}]*\}',    # 花括号内容
            r'\[[^\]]*\]',   # 方括号内容
            r'\([^)]*\)',    # 圆括号内容  
            r'<[^>]*>'       # 尖括号内容
        ]
        for pattern in tag_patterns:
            text = re.sub(pattern, '', text)
    else:
        text = re.sub(tag_pattern, '', text)
    return text
# 批量处理
def batch_process(input_files, output_folder, tag_pattern=None):
    for input_file in input_files:
        with open(input_file, 'r', encoding='utf-8') as f:
            content = f.read()
        cleaned = remove_custom_tags(content, tag_pattern)
        output_file = os.path.join(output_folder, os.path.basename(input_file))
        with open(output_file, 'w', encoding='utf-8') as f:
            f.write(cleaned)

使用Sed命令(Linux/Mac)

# 去除尖括号标记
sed -i 's/<[^>]*>//g' *.txt
# 去除方括号标记
sed -i 's/\[[^]]*\]//g' *.txt
# 去除花括号标记
sed -i 's/{[^}]*}//g' *.txt
# 批量处理当前目录所有txt文件
for file in *.txt; do
    sed -i 's/<[^>]*>//g' "$file"
done

使用PowerShell(Windows)

# 去除HTML标签
Get-ChildItem -Path "C:\input\" -Filter *.txt | ForEach-Object {
    $content = Get-Content $_.FullName -Raw
    $clean = $content -replace '<[^>]+>', ''
    Set-Content -Path "C:\output\$($_.Name)" -Value $clean
}
# 去除方括号内容
Get-ChildItem -Path "C:\input\" -Filter *.txt | ForEach-Object {
    $content = Get-Content $_.FullName -Raw
    $clean = $content -replace '\[[^\]]+\]', ''
    Set-Content -Path "C:\output\$($_.Name)" -Value $clean
}

使用说明

  1. 选择合适的方法:根据标记类型选择对应的脚本
  2. 备份原始文件:建议先备份,避免误操作
  3. 测试小样:先用少量文件测试效果
  4. 调整正则:根据实际标记格式调整正则表达式

您需要哪种具体的标记去除方案?可以提供您的文本样本,我可以给出针对性更强的脚本。

抱歉,评论功能暂时关闭!