正在搜索仅在上工作的文件

2024-10-01 13:45:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

import linecache
car=1
vehical=[]
for x in range (7): #run this 7 times
    car=car+3 #number plates are on every 3rd line
    to_add=linecache.getline('finesV2.txt', car) #take the line (car variable contains integer concerning which one)
    vehical.append(to_add) #add this to array
file = open("details.txt", "r")
car=0
for x in range (7): #run this 7 times
    searchfor=vehical[car][0:7] #load time to search file for
    print "searchfor",searchfor #for debugging
    for line in file: #run amount of lines that are in the file
        if searchfor in line: #check if item being searched for is in that line
            print line #print out the line
    car=car+1 #increase the car variable to search for next item in vehicle array on next run
file.close()

该程序输出以下内容:

searchfor GX99QME
GX99QME,Alex 123,test street

searchfor IL45LTQ
searchfor ZX46GSR
searchfor GN11ILW
searchfor IN82SSD
searchfor WE50JEY
searchfor QS26DLO

然而,我希望并期望程序能做的是搜索每个车牌号的文件,以加载驾驶员详细信息。我已确认搜索功能正在工作,并且我正在搜索的文件中的车牌号正确,因为如果第14行更改为:

if "IL45LTQ" in line:

然后程序返回:

searchfor GX99QME
IL45LTQ,Tom

searchfor IL45LTQ
searchfor ZX46GSR
searchfor GN11ILW
searchfor IN82SSD
searchfor WE50JEY
searchfor QS26DLO

理想情况下,我想要的是程序返回所有车牌的详细信息。有什么想法吗


Tags: thetorunin程序addforline