Mystery
Mystery

Reputation: 81

reading the lines in file from specific position

i want to read lines from specific position from a txt file. below is the output i have got

['Type : aa-aaaa-aaa']                    #output should not have this line
['consumed by bbbbbbbb : 0x0x0 WWWWW']    #output should not have this line
['Pppp CCCC TTTT                           MMMMM            SSSSSS Nonoo.']   #output should have this line
['']                                                                        #output should not have this line
['1  NIL fL-E 10UU (SPD+), 1000XXXXX (SPD) WEEEEEEEEEEEEE   CATTTTTTTTT']   #read the file from here
['44 10/100/1000BBBBB Ppppppp OOo E SSSSSS WS-XXXXX-RRRRR+I CATTTTTTTTT']
['44 10/100/1000BBBBB Ppppppp OOo E SSSSSS WS-XXXXX-RRRRR+I CATTTTTTTTT']
['44 10/100/1000BBBBB Ppppppp OOo E SSSSSS WS-XXXXX-RRRRR+I CATTTTTTTTT']
['44 10/100/1000BBBBB Ppppppp OOo E SSSSSS WS-XXXXX-RRRRR+I CATTTTTTTTT']
['44 10/100/1000BBBBB Ppppppp OOo E SSSSSS WS-XXXXX-RRRRR+I CATTTTTTTTT']  #till here

from the code i have tried: -

with open ('sh_mooooo.txt', 'r') as mooo_info:
    lines = moooo_info.readlines()
    for l in lines:
        if not l.isspace():
            storeSplit = [" ".join(l.split()[1:9])]
            print (storeSplit)

Expected output: -

Pppp CCCC TTTT                           MMMMM            SSSSSS Oono.   

1  NIL fL-E 10UU (SPD+), 1000XXXXX (SPD) WEEEEEEEEEEEEE   CATTTTTTTTT   
44 10/100/1000BBBBB Ppppppp OOo E SSSSSS WS-XXXXX-RRRRR+I CATTTTTTTTT
44 10/100/1000BBBBB Ppppppp OOo E SSSSSS WS-XXXXX-RRRRR+I CATTTTTTTTT
44 10/100/1000BBBBB Ppppppp OOo E SSSSSS WS-XXXXX-RRRRR+I CATTTTTTTTT
44 10/100/1000BBBBB Ppppppp OOo E SSSSSS WS-XXXXX-RRRRR+I CATTTTTTTTT
44 10/100/1000BBBBB Ppppppp OOo E SSSSSS WS-XXXXX-RRRRR+I CATTTTTTTTT  

thx for the help.! :-)

Upvotes: 1

Views: 534

Answers (1)

Adam.Er8
Adam.Er8

Reputation: 13393

just use slicing.

replace lines = moooo_info.readlines() with this:

lines = moooo_info.readlines()[2:]

it means "all except the first 2 element"

also, you seems like you wrap your strings a list with need.

try this:

with open ('sh_mooooo.txt', 'r') as mooo_info:
    lines = moooo_info.read().splitlines()[2:]
    for l in lines:
        if not l.isspace():
            storeSplit = " ".join(l.split())
            print (storeSplit)

Upvotes: 1

Related Questions