Improved https://www.cnblogs.com/FiaFia/p/9361225.html based on the previous version
This will be able to put the information needed in this line file one by one, written as CSV, no value is set to NULL
The problem that has not been solved is NewsItem = [Tradableid[0],exchange[0],symbol[0],name[0],isin[0],currency,instrumentsubtype,country, Securitytype[0],lotsize[0]]
No [0] in the CSV is displayed as ["309"], there is [0] in the CSV is the value, will look good
If it is set to [0], if there is no such value in this line, it will be reported 'indexerror:list index out of range '
Then we need to see if we can add a lambda function to determine if it is empty, and the null is set to "
ImportCSVImportRedefextract_all (filename): Result=[] Print('Extracting data----') Lines= open (filename,'R', encoding='UTF8', errors='Ignore'). ReadLines () forLineinchlines:if "BDt;" inchLine:tradableid=None Exchange=None Symbol=None name=None Isin=None Currency=None Instrumentsubtype=None Country=None SecurityType=None NewsItem=None Tradableid= Re.findall (r"; I (. +?);", line) Exchange= Re.findall (r"; Ex (. +?);", line) symbol= Re.findall (r"; SYm (. +?);", line) name= Re.findall (r"; NAm (. +?);", line) Isin= Re.findall (r"; ISn (. +?);", line) currency= Re.findall (r"; CUt (. +?);", line) Instrumentsubtype= Re.findall (r"; INt (. +?);", line) Country= Re.findall (r"; CNy (. +?);", line) SecurityType= Re.findall (r"; STy (. +?);", line) lotSize= Re.findall (r"; LSz (. +?);", line) NewsItem=[Tradableid[0],exchange[0],symbol[0],name[0],isin[0],currency,instrumentsubtype,country,securitytype[0], Lotsize[0]] Result.append (newsitem)returnresultdefwritetocsv (Newsitems, reportfile):Print('Start writing to CSV') ifNewsitems:with Open (reportfile, mode='w+', encoding='UTF8', errors='Ignore') as Csvfile:writer= Csv.writer (csvfile,lineterminator='\ n') Writer.writerow (['Tradableid','Exchange','symbol','Name','Isin','CurrentY','Instrumentsubtype','Country','SecurityType','lotSize']) forIinchRange (0, Len (newsitems)): Writer.writerow (Newsitems[i])Print('csv written done') returnif __name__=='__main__': FileName='xsto_stock_basicdata0725'data_file= filename +'. Tip'Reportcsv='Bdt_'+ filename +'. csv'Newsitemsum=Extract_all (data_file) writetocsv (newsitemsum, Reportcsv)
[Python] Extracts the string after the specified string in the file-2