如何基于关键词Line#将文本文件转换为指定格式CSV
问题分析与解决方案
问题概述
需要从包含日志、下载进度的文本文件中,提取以Line #开头的行号及其对应的文本内容,整理成指定格式的结构化文件(用户称CSV,实际为对齐式结构化文本)。
原始输入文本示例:
**********End Transcription Grammar correction log*********** Downloading: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.29k/1.29k [00:00<00:00, 480kB/s] Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████| 850M/850M [00:30<00:00, 29.0MB/s] Downloading: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████| 773k/773k [00:00<00:00, 3.66MB/s] Downloading: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.32M/1.32M [00:00<00:00, 7.99MB/s] Line # 1 I settled the first question: "I have fit you n if you could choose one word to describe your experience of being pregnant, what would it be otherwise it would be lucky." Line # 2 A because I had several pregnancy losses I'm prior to conceiving my daughter, and then this permetsii and I'm thinking about you, how do you think that things will change once you? Line # 3 become apparent again Amen I (Thinka) having two children is going to be a lot more busy I'm going to have to really focus on staying organized a because it's hard enough. Line # 4 With a toddler, O and my husband and I are going to really have to work as a team we already do, but it's going to take it to a different level. Line # 5 SM O Andte Have you thought about things like breast eating, sleeping arrangements or child cae, yes, and a if you could elaborate on is O sura so I planned to you. Line # 6 Nursing and My Daughter: My nurse to my my M toddler until she was about nine months UM and I plan to try to go as long as I can with this.
预期输出格式:
num text Line #1 I settled the first question: "I have fit you n if you could choose one word to describe your experience of being pregnant, what would it be otherwise it would be lucky." Line #2 A because I had several pregnancy losses I'm prior to conceiving my daughter, and then this permetsii and I'm thinking about you, how do you think that things will change once you? ... Line #n some text
现有代码的问题
- 拆分逻辑错误:用
split('Line #')后,第一个块包含开头的日志和下载内容,后续块是数字+换行+文本的组合,直接拼接会把行号和文本混在一起,无法正确配对。 - 未过滤无关内容:开头的日志、下载进度行没有被排除,会混入最终结果。
- 格式不符合预期:用
|连接行内容,没有实现行号与文本对齐的格式,也未处理文本换行后的缩进。
改进后的代码
# 读取文件内容,保留换行符避免文本丢失格式 with open("output.txt", 'r', encoding='utf-8') as txt_file: lines = [line.rstrip('\n') for line in txt_file] # 初始化表头 formatted_content = "num text\n\n" i = 0 while i < len(lines): current_line = lines[i] # 识别行号行 if current_line.startswith("Line #"): # 去除行号中的空格,统一格式为Line #n line_num = current_line.replace(" ", "") # 获取对应文本行(确保不越界) if i + 1 < len(lines): text = lines[i+1] # 处理长文本自动换行并缩进(按60字符拆分) max_line_length = 60 text_parts = [] for j in range(0, len(text), max_line_length): text_parts.append(text[j:j+max_line_length]) # 拼接行号和第一部分文本 formatted_line = f"{line_num:<12} {text_parts[0]}\n" # 拼接后续拆分的文本,保持缩进 for part in text_parts[1:]: formatted_line += f"{' ':12} {part}\n" # 添加到结果,每行之间空两行 formatted_content += formatted_line + "\n\n" # 跳过已处理的文本行 i += 2 continue # 非行号行,直接跳过 i += 1 # 写入文件 with open('formatted_content.txt', 'w', encoding='utf-8') as output_file: output_file.write(formatted_content)
代码说明
- 逐行过滤处理:只识别以
Line #开头的行,自动跳过无关的日志、下载进度行。 - 行号与文本配对:每识别到行号行,就取其下一行作为对应文本,确保关联关系正确。
- 格式对齐与换行:用格式化语法
{:<12}让行号左对齐并占12个字符宽度;对长文本自动拆分,后续行保持相同缩进,匹配预期格式。 - 编码兼容:指定
utf-8编码,避免特殊字符出现乱码。
内容的提问来源于stack exchange,提问作者user17993062
相关产品推荐
相关产品推荐

