[Tutor] Parse text with python

Albert-Jan Roskam fomcl at yahoo.com
Thu Oct 17 22:14:02 CEST 2013



On Thursday, October 17, 2013 9:54 PM, Danilo Chilene <bicofino at gmail.com> wrote:

Hello,
>
>I have the text below:
>
>CSMP0097I 14.42.12 CPU-B SS-BSS  SSU-AA   IS-02
> 
>-LAL USER CONTROL FILE-
>MAXIMUM DEFINABLE USERS    - 14999
>MAXIMUM CONFIGURABLE USERS - 11790
>CURRENT NUMBER USERS -       09692
> USER FLUSH CRET TIME VALUE -    5
>-LAL USER FILE-
>NAME             UORD T/O O OXIT RXIT S    MAX      INUSE   PCT
>---------------- ---- --- - ---- ---- - --------- --------- ---
>J4IB              305  15             A      1700      6666  95
> KEYWORDS - TA/J4IB CRTSST  DYNTAC  HSSPDB  
> DELTAS - OBTAIN    0  RELEASE    0 FLUSH VALUE    0
> DR CANDIDATE - N  REPORT - N
> INUSE DATE - 10.17.2013
>  
>END OF DISPLAY
>
>
>How I can grab the number 6666? My idea is to find the number 1700(this number never changes) and count 6 chars and the print the next 4 chars. But I didn't find *how to count chars and print next* on python.
>
>Any ideas?
This is one crude way, because the number 1700 might also be somewhere else in a line. Maybe (part of) that line could be written as a regular expression, e.g. J4IB *may* be [A-Z][0-9][A-Z]{2}\b

>>> import re
>>> lines = """CSMP0097I 14.42.12 CPU-B SS-BSS  SSU-AA   IS-02
 
-LAL USER CONTROL FILE-
MAXIMUM DEFINABLE USERS    - 14999
MAXIMUM CONFIGURABLE USERS - 11790
CURRENT NUMBER USERS -       09692
 USER FLUSH CRET TIME VALUE -    5
-LAL USER FILE-
NAME             UORD T/O O OXIT RXIT S    MAX      INUSE   PCT
---------------- ---- --- - ---- ---- - --------- --------- ---
J4IB              305  15             A      1700      6666  95
 KEYWORDS - TA/J4IB CRTSST  DYNTAC  HSSPDB 
 DELTAS - OBTAIN    0  RELEASE    0 FLUSH VALUE    0
 DR CANDIDATE - N  REPORT - N
 INUSE DATE - 10.17.2013
 
END OF DISPLAY""".split("\n")
>>> for line in lines:
    record = re.split("\s+", line)
    if len(record) >= 5 and record[4] == "1700":
        value_of_interest = record[5]
>>> value_of_interest
'6666'



More information about the Tutor mailing list