我如何从一串只包含数字的代码中删除行,同时留下带有数字和字母的代码行?

  • 本文关键字:数字 代码 删除行 一串 包含 python
  • 更新时间 :
  • 英文 :

0
1
00:00:00,210 --> 00:00:00,930
Hey,
1
2
00:00:00,930 --> 00:00:05,280
welcome to day 50 of your course
2
3

查看您提供的输入,我假设所讨论的文件是一个srt文件。在这种情况下,你很有可能在0 1部分犯了错误。如果是这种情况,您可以简单地使用isnumeric()方法来删除完全是数字的字符串。一个相同的例子是:

f = open("infile.srt", "r")
data = f.read().splitlines()
cleaned_data = []
for eachLine in data:
if not(eachLine.isnumeric()) and len(lineText)>1:
cleaned_data.append(eachLine) 
print(cleaned_data)

但是如果0 1是一个常见的外观,你可以使用一个额外的检查条件,没有空格,这可以用代码来完成:

f = open("infile.srt", "r")
data = f.read().splitlines()
cleaned_data = []
for eachLine in data:
lineText = eachLine.replace(" ", "")
if not(lineText.isnumeric()) and len(lineText)>1:
cleaned_data.append(eachLine) 
print(cleaned_data)

最新更新