Selecting first part of a text file in Python -


in process of making program, came across need read first value of text file. have tried put each value onto it's own line , read first line such in code, never load part of text file wanted shown in code:

l = open('output.txt').read()  words = l.split()  word in words:     print(word)  open("output.txt", 'wt').write(l)   open('output.txt','r') f:     line in f:         word in line.split():            print(word)  open("output.txt", 'wt').write(word)  q = open("output.txt")  x, line in enumerate(q):     if x == 0:         print (line) 

how able this?

store contents 1 element line , this

with open("input.txt") in_file:     data = next(in_file) 

now data have first line.

this works because open function returns object of file type, iterable. so, iterate next function first line.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -