【发布时间】:2018-06-29 15:09:30
【问题描述】:
我有一个 7000 多行的 .txt 文件,其中包含描述和图像的有序路径。示例:
abnormal /Users/alex/Documents/X-ray-classification/data/images/1.png
abnormal /Users/alex/Documents/X-ray-classification/data/images/2.png
normal /Users/alex/Documents/X-ray-classification/data/images/3.png
normal /Users/alex/Documents/X-ray-classification/data/images/4.png
缺少某些行。我想以某种方式自动搜索缺失的行。我直觉地写道:
f = open("data.txt", 'r')
lines = f.readlines()
num = 1
for line in lines:
if num in line:
continue
else:
print (line)
num+=1
但它当然没有用,因为行是字符串。 有什么优雅的方法可以解决这个问题吗?也许使用正则表达式? 提前致谢!
【问题讨论】:
-
使用
str(num)进行字符串比较?
标签: python string list file search