[Python] Extracts the string after the specified string in the file-2

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.