一、文档概述
本技术文档主要介绍的功能是从一个包含大量法规文本的 Compilation.txt 文件中,根据 headline.txt 文件中所列出的标题,将法规文本按标题进行分割,并将每个标题对应的内容分别保存为独立的 TXT 文件,输出到指定的目录中。
二、主要功能
(一)读取法规文本和标题信息
-
读取 Compilation.txt 文件
- 功能:从指定路径的 Compilation.txt 文件中读取全部文本内容,并将其按行存储在一个列表中,方便后续逐行处理。
- 实现方式:使用 Python 的内置函数
open()
以只读模式('r')打开文件,指定编码为 'utf - 8',然后使用readlines()
方法读取文件的所有行。
-
读取 headline.txt 文件中的标题
- 功能:从 headline.txt 文件中提取法规文件的标题信息。在提取过程中,会对每一行进行处理,去除前后的空白字符,跳过空行,同时按第一个空格分割,取分割后的第二部分作为标题并存储在一个列表中。
- 实现方式:同样使用
open()
函数打开文件,逐行读取,使用strip()
方法去除前后空格,利用split(' ', 1)
按第一个空格分割,将处理后的标题添加到headlines
列表中。
(二)创建输出目录
- 功能:检查指定的输出目录是否存在,如果不存在则创建该目录,确保后续生成的文件有合适的存储位置。
- 实现方式:使用 Python 的
os.path.exists()
函数检查目录是否存在,若不存在则使用os.makedirs()
函数创建目录。
(三)法规文本按标题分割与存储
-
遍历文档内容并匹配标题
- 功能:遍历 Compilation.txt 文件中的每一行文本,检查该行文本是否包含 headline.txt 文件中提取的标题。若包含标题,将之前存储的内容保存为一个文件,更新当前标题,并清空存储内容的列表,准备存储新标题下的内容。
- 实现方式:使用
for
循环遍历存储法规文本的列表,对每行文本使用strip()
去除前后空格,然后使用for
循环遍历标题列表,使用if headline in text
进行匹配判断。
-
保存分割后的法规文本
- 功能:当检测到新的标题时,将之前存储的文本内容保存为一个独立的 TXT 文件,文件名根据标题进行清理后生成。同时,在最后遍历结束后,将最后一个标题对应的文本内容也保存为文件。
- 实现方式:使用
clean_filename()
函数对标题进行清理(确保文件名合法),使用os.path.join()
函数构建文件的完整路径,使用open()
函数以写入模式('w')打开文件,使用write()
方法将存储的文本内容写入文件。
三、输出与日志
在整个处理过程中,会输出一些日志信息,帮助用户了解程序的执行情况。主要包括提取的标题信息、找到新标题的提示以及保存文件的路径信息。例如,会打印出 “Headlines extracted: [标题列表]”、“Found new title: [标题名称]” 和 “Saved [文件路径]” 等信息。
四、总结
该功能主要实现了法规文本的分割与存储,通过读取 Compilation.txt 文件和 headline.txt 文件,将法规文本按标题进行分割,并将每个标题对应的内容保存为独立的 TXT 文件,方便后续对法规文本进行分类管理和使用。同时,通过日志信息的输出,用户可以清晰地了解程序的执行过程和结果。
完整代码
import os
import re
def clean_filename(filename):
# 去除非法字符
filename = re.sub(r'[\\/*?:"<>|]', '', filename)
# 去除多余的空格
filename = filename.strip()
return filename
def extract_content_by_headlines(compilation_file_path, headline_file_path, output_dir):
# 读取Compilation.txt文件中的内容
with open(compilation_file_path, 'r', encoding='utf-8') as compilation_file:
full_text = compilation_file.readlines()
# 读取headline.txt文件中的标题
headlines = []
with open(headline_file_path, 'r', encoding='utf-8') as headline_file:
for line in headline_file:
line = line.strip()
if line: # 跳过空行
parts = line.split(' ', 1) # 按第一个空格分割
if len(parts) > 1: # 确保有标题部分
headlines.append(parts[1].strip())
print("Headlines extracted:", headlines)
# 创建输出目录(如果不存在)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# 初始化变量
current_section = []
current_title = None
# 遍历文档内容
for text in full_text:
text = text.strip() # 去除前后空格
# 检查当前文本是否匹配任何标题
matched = False
for headline in headlines:
if headline in text: # 更灵活的匹配逻辑
if current_title is not None:
# 清理文件名
clean_title = clean_filename(current_title)
# 保存当前部分
file_path = os.path.join(output_dir, f"{clean_title}.txt")
with open(file_path, 'w', encoding='utf-8') as txt_file:
txt_file.write("".join(current_section).strip())
print(f"Saved {file_path}")
# 更新当前标题
current_title = headline
current_section = []
matched = True
print(f"Found new title: {current_title}")
break
if not matched:
# 如果没有匹配到标题,添加内容到当前部分
current_section.append(text)
# 保存最后一个部分
if current_title is not None:
clean_title = clean_filename(current_title)
file_path = os.path.join(output_dir, f"{clean_title}.txt")
with open(file_path, 'w', encoding='utf-8') as txt_file:
txt_file.write("".join(current_section).strip())
print(f"Saved {file_path}")
# 使用示例
compilation_file_path = r"D:\MyPythonProject\Word_TXT\Compilation.txt"
headline_file_path = r"D:\MyPythonProject\Word_TXT\headline.txt"
output_dir = r"D:\MyPythonProject\Word_TXT\output_txt"
extract_content_by_headlines(compilation_file_path, headline_file_path, output_dir)