linux,请用python程序,把.srt转换成.txt?
- User
linux,请用python程序,把.srt转换成.txt?
- Assistant
可以使用Python编写一个简单的脚本,把.srt(字幕文件)中的文本提取出来并保存到.txt文件中。字幕文件中的时间戳和其他格式信息将被忽略,只保留实际的字幕文本。以下是一个示例脚本:
import re def srt_to_txt(srt_file, txt_file): with open(srt_file, 'r', encoding='utf-8') as infile, open(txt_file, 'w', encoding='utf-8') as outfile: content = infile.read() # Remove index numbers content = re.sub(r'\d+\n', '', content) # Remove timestamps content = re.sub(r'\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}\n', '', content) # Remove empty lines and consolidate text content = re.sub(r'\n{2,}', '\n', content.strip()) # Write cleaned text to output file outfile.write(content) # 调用函数,把.srt文件转换成.txt文件 srt_to_txt('example.srt', 'output.txt')此脚本执行以下操作:
- 打开.srt文件并读取其内容。
- 移除表示序号的数字行。
- 移除时间戳信息。
- 移除多余的空行,合并连续的文本行。
- 将处理后的文本写入输出的.txt文件。
请确保你已经安装了Python并且你所使用的文件路径正确。这个脚本假设.srt文件是UTF-8编码,如果不是,请修改编码设置以匹配实际编码。
- User
pip install re Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple ERROR: Could not find a version that satisfies the requirement re (from versions: none) ERROR: No matching distribution found for re 为什么?
- Assistant
re是 Python 标准库中的一个模块,用于处理正则表达式。你不需要通过pip安装re模块,因为它已经包含在Python的标准库中,只需直接导入并使用即可。可能是你对Python的模块管理有点混淆,但别担心,Python标准库中的模块如
re、os、sys等都是无需单独安装的。你只要在代码中直接导入它们就可以了。你可以验证这一点,通过运行以下简单的Python脚本:
import re # 虚拟的一段需要处理的字符串 sample_text = "123\n00:01:02,000 --> 00:01:05,000\nThis is a subtitle line." # 使用正则表达式来移除时间戳 result = re.sub(r'\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}\n', '', sample_text) print(result)如果你可以顺利运行上述脚本并看到时间戳被移除,那就说明你的Python标准库中的
re模块是可以正常工作的。请直接使用之前提供的代码示例来转换
.srt文件为.txt文件,而无需进行任何额外的模块安装。再次总结整个流程:- 确保你有Python安装在你的系统上。
- 使用你的文本编辑器复制那个代码示例。
- 将代码保存为一个Python脚本文件,例如
srt_to_txt.py。 - 在终端或命令提示符中运行这个脚本:
python srt_to_txt.py
如果你有任何进一步的问题,请随时提问。
- User
python的re模块如何使用?
- Assistant
Python 的
re模块用于正则表达式操作,它允许你搜索、匹配和操作字符串。以下是一些常见的用途和方法:1. 导入模块
首先,导入
re模块:import re - User
linux,请用python程序,把.srt转换成.txt?
- Assistant
要在Linux系统上使用Python脚本把
.srt(字幕文件)转换成.txt(纯文本文件),可以使用以下代码。这个脚本将清理掉字幕文件中的序号和时间戳,只保留实际的字幕文本内容。Python脚本示例
import re import sys def srt_to_txt(srt_file, txt_file): try: with open(srt_file, 'r', encoding='utf-8') as infile, open(txt_file, 'w', encoding='utf-8') as outfile: content = infile.read() # 移除序号 content = re.sub(r'\d+\n', '', content) # 移除时间戳 content = re.sub(r'\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}\n', '', content) # 移除多余的空行 content = re.sub(r'\n{2,}', '\n', content.strip()) # 写入处理后的内容到输出文件 outfile.write(content) print(f"Converted {srt_file} to {txt_file} successfully.") except Exception as e: print(f"An error occurred: {e}") if __name__ == '__main__': if len(sys.argv) != 3: print("Usage: python srt_to_txt.py <input.srt> <output.txt>") else: srt_file = sys.argv[1] txt_file = sys.argv[2] srt_to_txt(srt_file, txt_file) - User
python语句content = re.sub(r'\n{2,}', '\n', content.strip())是什么意思?
- Assistant
Python 语句
content = re.sub(r'\n{2,}', '\n', content.strip())主要用于处理文本中的多余空行。让我们逐个部分解析这个语句的作用:1.
content.strip().strip()方法用于移除字符串首尾的空白字符,包括空格和换行符。- 举例:对于字符串
" Hello World \n", 调用.strip()将返回"Hello World"。
2. 正则表达式
r'\n{2,}'r'...'是原始字符串(raw string)表示法,表示字符串里的反斜杠\不需要转义。'\n'表示换行符。'{2,}'表示前一个字符(在这里是换行符\n)至少出现2次以上。- 整个
\n{2,}表示匹配连续的两个或更多的换行符。
3.
re.sub(r'\n{2,}', '\n', content)re.sub(pattern, repl, string)是re模块中的一个函数,用于替换字符串中所有匹配正则表达式pattern的内容为repl。pattern是我们要匹配的正则表达式:\n{2,}。repl是我们希望替换为的内容:在这里是单个换行符 。
- User
linux,请用python程序,把.srt转换成.txt?
- Assistant
好的,这里是一个具体的Python脚本,能够在Linux环境下将
.srt文件转换成纯文本的.txt文件。这个脚本会读取.srt文件的内容,移除序号和时间戳,只保留实际的字幕文本内容,然后将结果保存到一个新的.txt文件中。Python脚本示例
将下面的代码保存到一个Python脚本文件中,比如
srt_to_txt.py:import re import sys def srt_to_txt(srt_file, txt_file): try: with open(srt_file, 'r', encoding='utf-8') as infile, open(txt_file, 'w', encoding='utf-8') as outfile: content = infile.read() # 移除序号 content = re.sub(r'\d+\n', '', content) # 移除时间戳 content = re.sub(r'\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}\n', '', content) # 移除多余的空行 content = re.sub(r'\n{2,}', '\n', content.strip()) # 写入处理后的内容到输出文件 outfile.write(content) print(f"Converted {srt_file} to {txt_file} successfully.") except Exception as e: print(f"An error occurred: {e}") if __name__ == '__main__': if len(sys.argv) != 3: print("Usage: python srt_to_txt.py <input.srt> <output.txt>") else: srt_file = sys.argv[1] txt_file = sys.argv[2] srt_to_txt(srt_file, txt_file) - User
content = re.sub(r’\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}\n’, ‘’, content)这个语句无效,输出了00:01:07,040 --> 00:01:11,morning is just talk a little bit about how 这样的结果,为什么?
- Assistant
问题有可能出在以下几个方面:
- 引号类型不匹配:确保你使用的是标准的单引号或双引号,而不是非标准的 Unicode 引号。
- 正则表达式的准确性:确保正则表达式准确匹配了时间戳格式,同时注意时间戳后的换行符可能有不同的表现形式。
修正后的代码
import re def srt_to_txt(srt_file, txt_file): try: with open(srt_file, 'r', encoding='utf-8') as infile, open(txt_file, 'w', encoding='utf-8') as outfile: content = infile.read() # 移除序号 content = re.sub(r'\d+\n', '', content) # 移除时间戳(注意这里使用标准的引号) content = re.sub(r'\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}\s*', '', content) # 移除多余的空行 content = re.sub(r'\n{2,}', '\n', content.strip()) # 写入处理后的内容到输出文件 outfile.write(content) print(f"Converted {srt_file} to {txt_file} successfully.") except Exception as e: print(f"An error occurred: {e}") if __name__ == '__main__': import sys if len(sys.argv) != 3: print("Usage: python srt_to_txt.py <input.srt> <output.txt>") else: srt_file = sys.argv[1] txt_file = sys.argv[2] srt_to_txt(srt_file, txt_file) - User
还是这样,原始数据是这样的4 00:00:29,800 --> 00:00:33,360 introduction and for having me. You know, Ralph, when you when you invited me to
- Assistant
理解了。那么让我们仔细检查和确保我们完全地匹配并移除时间戳。这是一个更加详细的实现,其中我还会加入更多的调试信息,以确保处理内容的每一步都能正确地执行。
修正后的完整代码
import re import sys def srt_to_txt(srt_file, txt_file): try: with open(srt_file, 'r', encoding='utf-8') as infile, open(txt_file, 'w', encoding='utf-8') as outfile: content = infile.read() # 输出原始内容用于调试 print("Original content:") print(content) # 移除序号 content = re.sub(r'^\d+$', '', content, flags=re.MULTILINE) print("\nAfter removing indexes:") print(content) # 移除时间戳 content = re.sub(r'\d{2}:\d{2}:\d{2},\d{3} --> \d{2}:\d{2}:\d{2},\d{3}', '', content) print("\nAfter removing timestamps:") print(content) # 移除多余的空行 content = re.sub(r'\n{2,}', '\n', content.strip()) print("\nAfter removing extra newlines:") print(content) # 写入处理后的内容到输出文件 outfile.write(content) print(f"\nConverted {srt_file} to {txt_file} successfully.") except Exception as e: print(f"An error occurred: {e}") if __name__ == '__main__': if len(sys.argv) != 3: print("Usage: python srt_to_txt.py <input.srt> <output.txt>") else: srt_file = sys.argv[1] txt_file = sys.argv[2] srt_to_txt(srt_file, txt_file)