In python how we can select second line string of a text file.
I have a text file and in that file there are some data in line by line_
Dog
Cat
Cow
How to fetch the second line which is “Cat” and store that string in a variable.
f = open(“text_file.txt”, “r”)
var = # “Cat”
Can I use single line code without using any loop or anything like we open a file using this single line of code f = open(“text_file.txt”, “r”)
var = f.readlines()[1]readlines()[1]will give you the 2nd line of the file, not the 1st:readlines()[0]will give you the 1st. But now it appears you wanted to get several lines from the file, not just one. That can't be done in a single line because you can only callreadlines()once on an open file. The first time reads all of the lines in the file and gives you back a list of them. That uses the lines up, and callingreadlines()again will give you an empty list. So:mylist = f.readlines(); apikey=mylist[0]; client_token=mylist[1]etc.