question No. 0014: Plain text file Student.txt for student information, the contents (including curly braces) are as follows:
{ " : [ Zhang San , 150,120,100", " 2 " : [ John Doe " , 90,99,95], 3 : [ Harry , 60,66,68"}
Please write the above into the Student.xls file as shown in the following example:
This topic uses the Python third-party library XLWT mentioned earlier. (=-=pip is true once and for all)
Code:
ImportReImportXLWTdefRead2xls (x): DataTable= XLWT. Workbook (encoding ='Utf-8', style_compression =0) NewSheet= Datatable.add_sheet ('Student', CELL_OVERWRITE_OK =True) Num=0 with open (x,'R') as F:text=f.read () info= Re.compile (r'" (\d+)": \["(. *?)", (\d+), (\d+), (\d+)]') forXinchInfo.findall (text): forIinchRange (len (x)): Newsheet.write (num, I, x[i]) num+ = 1Datatable.save ('Liez.xls') Read2xls ('Student.txt')
Effect:
Note:
1. Workbook and Addsheet New The new one, according to the document, is
2.0004 the RE library used to haunt again, compile conversion format, but also regular expression , not familiar with the document reluctantly written out, incredibly can use, Koko. (“.*?” refers to unlimited characters (strings), "\d+" knows numbers (strings))
3. Write () function, in the same row as count number of rows num, write line number I, content x[i], remember count Num plus 1
"Python Mini Practice" 0014