python提取txt信息,处理,输出
1. 文件路径设定及文件遍历
import os path = 'E:\\Python36\\testdata' os.chdir(path) os.listdir() for filename in os.listdir(): file = open(filename, 'r') for eachline in file.readlines(): # process eachline
2. 提取glass ID
IDlist = [] file = open('A1770266172.txt', 'r') for line in file.readlines(): if line.startswith('Process'): ID = line[37: ] IDlist.append(ID) print(IDlist)
3. 提取Input/Buffer/Inspection/Printer/UvLevel/Output时间
for line in file.readlines(): if line.startswith('Arrived at Input'): InputIN = line[20:39] elif line.startswith('Departed at Input'): InputOUT = line[18:37] elif line.startswith('Arrived at Buffer'): BufferSLOT = line[17:18] BufferIN = line[22:41] elif line.startswith('Departed at Buffer'): BufferOUT = line[20:39] elif line.startswith('Arrived at Inspection'): InspectionIN = line[25: ] elif line.startswith('Departed at Inspection'): InspectionOUT = line[23:42] elif line.startswith('Arrived at Printer'): PrinterIN = line[22: ] elif line.startswith('Departed at Printer'): PrinterOUT = line[20:39] elif line.startswith('Arrived at UvLevel'): UVSLOT = line[18:19] UVIN = line[23: ] elif line.startswith('Departed at UvLevel'): UVOUT = line[20: ] elif line.startswith('Arrived at Output'): OutputIN = line[21: ] elif line.startswith('Departed at Output'): OutputOUT = line[19:38] else: InputIN = 'NA'
4. 把提取的信息存入文件中
import csv with open('E:\\Python36\\glass.csv', 'w', newline='') as csvfile: fieldnames1 = ['glass ID', 'input in', 'input out', 'buffer in', 'buffer out', 'aoi in', 'aoi out', 'printer in', 'printer out', 'uv in', 'uv out', 'output in', 'output out', 'buffer slot', 'uv level'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames1) writer.writeheader() for item in list_dict: writer.writerow(item)
5. 整合完整代码
import os path = 'E:\\Python36\\testdata' os.chdir(path) os.listdir() glass = [] list_dict = [] for filename in os.listdir(): a=b=c=d=e=f=g=h=i=j=k=l=m= 0 file = open(filename, 'r') for line in file.readlines(): if line.startswith('Process'): a = a+1 if a < 2: ID = line[37: -1] glass.append(ID) else: continue elif line.startswith('Arrived at Input'): b = b+1 if b < 2: InputIN = line[20:39] glass.append(InputIN) else: continue elif line.startswith('Departed Input'): c = c +1 if c < 2: InputOUT = line[18:37] glass.append(InputOUT) else: continue elif line.startswith('Arrived at Buffer'): d = d+1 if d <2: BufferSLOT = line[17:18] glass.append(BufferSLOT) BufferIN = line[22:41] glass.append(BufferIN) else: continue elif line.startswith('Departed Buffer'): e = e+1 if e<2: BufferOUT = line[20:39] glass.append(BufferOUT) else: continue elif line.startswith('Arrived at Inspection'): f = f+1 if f <2: InspectionIN = line[25: -1] glass.append(InspectionIN) else: continue elif line.startswith('Departed Inspection'): g = g+1 if g<2: inspectionOUT = line[23:42] glass.append(inspectionOUT) else: continue elif line.startswith('Arrived at Printer'): h =h+1 if h<2: PrinterIN = line[22: -1] glass.append(PrinterIN) else: continue elif line.startswith('Departed Printer'): i = i+1 if i<2: PrinterOUT = line[20:39] glass.append(PrinterOUT) else: continue elif line.startswith('Arrived at UvLevel'): j = j+1 if j<2: UVSLOT = line[18:19] glass.append(UVSLOT) UVIN = line[23: -1] glass.append(UVIN) else: continue elif line.startswith('Departed UvLevel'): k =k+1 if k<2: UVOUT = line[21: -1] glass.append(UVOUT) else: continue elif line.startswith('Arrived at Output'): l = l+1 if l<2: OutputIN = line[21: -1] glass.append(OutputIN) else: continue elif line.startswith('Departed Output'): m = m+1 if m<2: OutputOUT = line[19:38] glass.append(OutputOUT) else: continue dic = {'glass ID':ID, 'input in':InputIN, 'input out':InputOUT, 'buffer slot':BufferSLOT, 'buffer in':BufferIN, 'buffer out':BufferOUT, 'aoi in':InspectionIN, 'aoi out':inspectionOUT, 'printer in':PrinterIN, 'printer out':PrinterOUT, 'uv level':UVSLOT, 'uv in':UVIN, 'uv out':UVOUT, 'output in':OutputIN, 'output out':OutputOUT} #print(dic) list_dict.append(dic) import csv with open('E:\\Python36\\glass.csv', 'w', newline='') as csvfile: fieldnames1 = ['glass ID', 'input in', 'input out', 'buffer in', 'buffer out', 'aoi in', 'aoi out', 'printer in', 'printer out', 'uv in', 'uv out', 'output in', 'output out', 'buffer slot', 'uv level'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames1) writer.writeheader() for item in list_dict: writer.writerow(item)
posted on 2017-09-09 23:06 你的踏板车要滑向哪里 阅读(1147) 评论(0) 编辑 收藏 举报