我试图解析一个文本文件,如果它包含,如“汽车,自行车,汽车”这样的词,如果这些词(可能会超过3个)存在,那么它必须打印为“燃料运行”和下一行
程序: 它给我的错误
with open(qwer.txt) as f:
vehicle=("car","bike","automobile")
for line in f:
for part in line.split():
if vehicle in part:
print line.index(key)+1
print "runs on fuel"
请帮助解答赞赏!
答案 0 :(得分:0)
套装最适合此类事物
with open("qwer.txt") as f:
vehicle=set(("car","bike","automobile"))
for line in f:
match = vehicle.intersection(line.split())
if match:
print "%s runs on fuel" % match.pop()