Python parsing text File Demo sample

Source: Internet
Author: User

Purpose: Find the first floating-point number in the text with the lower three vertical bars in the Sum/avg line

Idea: First use Python to read a line in the text and then cut the string. Finds rows that contain Sum/avgkeyword. Take out the desired result

Text Local:

........

|    Msop0 | 8 326 |   99.4 0.3 0.3 0.3 0.9 25.0 | -0.383 |
|--------+-------------+-----------------------------------------+-------------|
|    MSPE0 | 8 394 |100.0 0.0 0.0 0.0 0.0 0.0 |-2147483.648 |
|--------+-------------+-----------------------------------------+-------------|
|    Muor0 | 8 458 |100.0 0.0 0.0 0.0 0.0 0.0 |-2147483.648 |
|--------+-------------+-----------------------------------------+-------------|
|    mvxz0 | 8 435 |    99.8 0.0 0.2 0.2 0.5 25.0 | 0.419 |
|--------+-------------+-----------------------------------------+-------------|
|    Mwie0 | 8 376 |100.0 0.0 0.0 0.0 0.0 0.0 |-2147483.648 |
|==============================================================================|
|  Sum/avg| 224 11172 |   99.8 0.1 0.1 0.1 0.2 8.5 | -0.089 |
|==============================================================================|
|  Mean | 8.0 399.0 | 99.8 0.1 0.1 0.1 0.2 8.5 |-2147483.648 |
|  S.D.  |  0.0 35.4 | 0.2 0.1 0.2 0.1 0.3 9.0 |-2147483.648 |
|  Median | 8.0 394.0 |100.0 0.0 0.0 0.0 0.2 12.5 |-2147483.648 |
'------------------------------------------------------------------------------'

Test procedures such as the following:

>>> ss= ' | Sum/avg|  224  11172 | 99.8    0.1 0.1 0.1 0.2 8.5    |   -0.089    | ' >>> SS ' | Sum/avg|  224  11172 | 99.8    0.1 0.1 0.1 0.2 8.5    |   -0.089    | ' >>> ss.split (' | ') [', ' sum/avg ', '  224  11172 ', ' 99.8 0.1 0.1 0.1 0.2 8.5    ', '   -0.089    ', ']>>> ss.split (' | ') [1] = = ' Sum/avg ' true>>> ss.split (' | ') [3] ' 99.8    0.1    0.1    0.1    0.2    8.5 ' >>> ss.split (' | ') [3] [0:5] ' 99.8 ' >>> float (ss.split (' | ') [3] [0:5]) 99.8

The procedure of Examination:
f = open ("Ctm_39phn.filt.sys")             # Returns a file object line = F.readline ()             # Call the file's ReadLine () method while line:    #print line,< c3/># followed by ', ' will ignore newline character    if Line.split (' | ') [1] = = ' Sum/avg ':p rint float (line.split (' | ') [3] [0:5])    # print (line, end = ') # in Python 3 using line    = F.readline () f.close ()

Python parsing text File Demo sample

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.