# ch14_18.py #fn = 'ch14_15.txt' # 設定欲開啟的檔案 fn = 'D:\Python_jdwang\Temp\TDCS_M06A_20210224_180000.csv' VehicleTypes= {"31":0, "32":0, "41":0, "42":0, "5":0} cnt = 0 with open(fn) as file_Obj: # 用預設mode=r開啟檔案,傳回檔案物件file_Obj for line in file_Obj: # 逐行讀取檔案到變數line Items = line.split(',') #if (line.find('03F2100S') != -1): if (Items[2].find('03F2100S') != -1): print(str(cnt)+ "=>"+line) # 輸出變數line相當於輸出一行 cnt = cnt+1 VT = Items[0] # 第一個欄位 VehicleTypes[VT] = VehicleTypes[VT] + 1 print(cnt) print("31=>"+str(VehicleTypes["31"])) print(VehicleTypes["32"]) print(VehicleTypes["41"]) print(VehicleTypes["42"]) print(VehicleTypes["5"])