代码:
def is_line_with_only_digits(line): # 检查一行是否只包含数字 return line.strip().isdigit() def process_file(input_file_path, output_file_path): with open(input_file_path, 'r', encoding='utf-8') as file: lines = file.readlines() with open(output_file_path, 'w', encoding='utf-8') as file: for line in lines: if is_line_with_only_digits(line): # 如果一行只包含数字,在它前面添加一个换行符 file.write('\n' + line) else: file.write(line) # 调用函数,替换下面的路径为你的文件路径 input_file_path = 'zhong.srt' output_file_path = 'zhong_new.srt' process_file(input_file_path, output_file_path)