如何从文件中删除特定文本?
How do i remove specific text from the file?
我试图从此字幕文件中删除一些特定的上下文,但是当我尝试这样做时,它会将文件完全擦除成空白。请帮我。谢谢。
x=open("text.txt",'rb')for sub in x:
sub=sub.strip()
if sub.startswith('0',0,28):
x.remove()
//字幕文件
您可以使用正则表达式来替换它。
正则表达式模式:[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)
打开文件并将文件内容存储在字符串中:
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)
我试图从此字幕文件中删除一些特定的上下文,但是当我尝试这样做时,它会将文件完全擦除成空白。请帮我。谢谢。
x=open("text.txt",'rb')for sub in x:
sub=sub.strip()
if sub.startswith('0',0,28):
x.remove()
//字幕文件
您可以使用正则表达式来替换它。
正则表达式模式:[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)
打开文件并将文件内容存储在字符串中:
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)