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
Post a Comment