我有29个.srt文件。它们都包含HTML代码,如<font color="#E5E5E5">
、<font color="#CCCCCC">
和</font>
。我想从这29个文件中去掉所有的HTML代码。但我不知道怎么一下子做到。我已经附上了我现在在这个问题上使用的代码。但它一次只能更改一个文件,它留给我3个无用的文件。有人能帮我吗
import re
string = open('/Users/Cynthia/Desktop/Jeunesse/Longivity English/Jeunesse
Longevity TV - Episode 27 - Lifestyle - PART 4 - Healthy Nutrition 2
2.en.transcribed.txt').read()
new_str = re.sub('<font color="#CCCCCC">', ' ', string)
open('b.txt', 'w').write(new_str)
string = open('/Users/Cynthia/Desktop/Jeunesse/Longivity
English/b.txt').read()
new_str = re.sub('<font color="#E5E5E5">', ' ', string)
open('c.txt', 'w').write(new_str)
string = open('/Users/Cynthia/Desktop/Jeunesse/Longivity
English/c.txt').read()
new_str = re.sub('</font>', ' ', string)
open('d.txt', 'w').write(new_str)
下面是一个初学者友好的方法,使用函数来处理一个文件。它链接您的代码并将结果写入一个新文件
因此,只需为每个文件调用strip\u html一次,其中包含新文件的文件名和名称
在本例中,有一个文件名列表,它将用“.fixed”这个词在结尾写入固定文件
请注意,这是一个简单的方法,我遗漏了很多东西,使之易于理解。一旦你对编程有了更多的了解,你就会找到更好的方法。但你应该让它起作用
希望这有帮助
运行此命令时,请查看os.listdir()命令,以了解如何从目录中获取文件名列表,而不是将其写入代码中
相关问题 更多 >
编程相关推荐