我正在尝试从此字幕文件中删除某些特定的上下文,但是当我尝试这样做时,它会将文件完全擦除为空白。请帮我。谢谢。
x=open("text.txt",'rb')for sub in x:
sub=sub.strip()
if sub.startswith('0',0,28):
x.remove()
//字幕文件
答案 0 :(得分:1)
您可以使用正则表达式替换它。
正则表达式模式:[0-9]+\n[0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]+ --> [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]+\n
import re
with open("text.txt") as file:
txt = re.sub(r"[0-9]+\n[0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]+ --> [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]+\n", "", file.read())
print(txt)
text.txt的内容示例
1
00:00:26,720 --> 00:00:31,720
Subtitles by <font color="#ff0000">explosiveskull</font>
Sync by <font color="#00ffff">GoldenBeard</font>
2
00:00:43,752 --> 00:00:45,621
(MEN CHATTERING INDISTINCTLY)
输出
Subtitles by <font color="#ff0000">explosiveskull</font>
Sync by <font color="#00ffff">GoldenBeard</font>
(MEN CHATTERING INDISTINCTLY)
答案 1 :(得分:0)
打开文件并将文件内容存储在字符串中:
with open("test.txt") as x:
data = x.read()
现在,您可以根据需要删除/向该字符串添加任何数据:
// operations that you wish to perform on the content
在w +模式下再次将其打开,并将修改后的内容写入其中:
with open("test.txt", w+) as x:
data = x.write(content)