"Python Mini Practice" 0014

Source: Internet
Author: User

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

Related Article

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.