简体   繁体   English

从特定位置读取文件中的行

[英]reading the lines in file from specific position

i want to read lines from specific position from a txt file.我想从 txt 文件中的特定位置读取行。 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.!谢谢你的帮助。! :-) :-)

just use slicing.只需使用切片。

replace lines = moooo_info.readlines() with this:用这个替换lines = moooo_info.readlines()

lines = moooo_info.readlines()[2:]

it means "all except the first 2 element"它的意思是“除了前 2 个元素之外的所有元素”

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM