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)
推荐阅读
- sql - ORA-01722: 无效的数字 where 子句
- django - Django - TestCase - response.status 代码失败
- r - 提取r中具有相同名称的子列表的所有元素
- python - 比较元组中的项目并在Python中返回项目的索引
- ibm-midrange - IBM 中的 CDC 引擎,我无法启动 TCP 侦听器作业
- javascript - 这个具有异步操作的 Javascript 程序的输出应该是什么?
- security - 在 ZAP 报告警报中获取无效计数
- python - Python:连续超过一定水平
- pandas - 熊猫创建一个具有其他值的新列
- css - 仅更改速记属性的一部分