需要从原始文件中把BDt;开头的那行中的NAm 抽取出来
Sample
期望结果:把 TEL8U07EP75.0000R0.100SWE 抽取出来
BDTe;i1269;Si4294967296;s8;TEt2;SYmEM;NAmExceptional circumstances market maker;
BDt;i2486558;Si144097;s8;Ex72;Mk806;INi126369;SYmTEL8U 75SWE;NAmTEL8U07EP75.0000R0.100SWE;SNmTEL8U 75SWE;ISnSE0010413013;ISi3540;ISsSWED;CUiSEK;CUtSEK;PRt1;VOd1;LDa20170921;Cf1;LPeN;TTd20180907;CFcRWXTPE;INt392;ITSz63;NDp4;NDc4;NDTp4;NDTc4;CLId8046;CNySE;ITStN;SSc2;STy2;AUmY;TRaY;INrY;PTaY;PTb2;OXCl0;RLoY;PMa;LSz1;LPl8162;DSchttp\://public.fsb.solutions.six.se/fsb.public/site/warrants/warrantslist.page;EUs1226;
代码
import re import csv def extract_BDt(input_file): file = open(input_file, 'r') new_file = open('BDtList.txt', 'w') for line in file: if "BDt;" in line: new_file.write(line) file.close() new_file.close() print('BDt is extracted') def extract_name(): file = open('BDtList.txt', 'r') with open('NameList.csv', 'w') as csv_file: writer = csv.writer(csv_file, lineterminator='\n') writer.writerow(["Name"]) for line in file: Name = re.findall(r"NAm(.+?);",line) writer.writerow(Name) print('Name is extracted') file.close() if __name__ == '__main__': data_file = 'C:\\Python36\\TestCode\\ExtractName\\Test.tip' print('**********Start***********') extract_BDt(data_file) extract_name() print('**********Done***********')
遇到的问题:输出的csv文件总会生成多余的一行,把 writer = csv.writer(csv_file) 改成writer = csv.writer(csv_file, lineterminator='\n') ,就解决这个问题了