How to read multiple specific lines from a txt file (python) -


the question:

i trying have program read multiple, specific lines text file. have gotten stage able program read multiple lines have add them , when printed have '[],[]' around them.

here current code:

import time time.sleep(3) one=1 while one==1:     = open("solution.txt","r").readlines()     line = a[1].split()     line2 = a[0].split()     print(line+line2)     one=0     time.sleep(3) exit() 

side note

i understand there similar questions although they're complicated. i'd simple answer. using python 3.5 , not noob nor proffessional. thank help!

if want read lines file can use loop so:

lines = [] line in enumerate(open("solution.txt", "r")):     lines.append(line) 

this create tuple containing line number (starting @ zero) , contents of line, every line in file. add each line number , line tuple list called lines.

if want specific lines (eg. line 5 or line 7) can use loop go through contents of lines.

targetlines = [5, 7] line in lines:     if line[0] in targetlines:         print(line[1]) 

this print contents of line 5 , 7, on separate lines. note still have newline character on end of them , may need print .rstrip() remove newline character.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -