上次按照个人需要整了一个JSON文件转换txt文档的代码,发现大家比较认可,确实帮助到了大家吧。今天整理了一个ASS格式转换txt文档的代码,具体如下:
def extract_chinese_content(ass_file_path):
chinese_content = []
with open(ass_file_path, 'r', encoding='utf-8') as file:
for line in file:
if line.startswith('Dialogue:'):
fields = line.split(',', maxsplit=9)
content = fields[9].strip()
chinese_content.append(content)
return chinese_content
# 读取文件位置
ass_file_path = r'C:/Users/Administrator/Downloads/纪录片·【中国】从盛唐到近代(第二季·全10集) - 1.第一集:惊变.ass'
chinese_content = extract_chinese_content(ass_file_path)
for idx, content in enumerate(chinese_content, start=1):
print(f"Content {idx}: {content}")
def save_to_text(chinese_content, output_txt_path):
with open(output_txt_path, 'w', encoding='utf-8') as txt_file:
for idx, content in enumerate(chinese_content, start=1):
txt_file.write(content + ', ')
# TXT文本保存路径
output_txt_path = 'output_file.txt'
save_to_text(chinese_content, output_txt_path)
大家还是根据自己的情况修改文件目录就好了,默认保存文件的地方就是代码的当前位置。感谢大家能够喜欢。