How to read a specific line from a text file in Python
This tutorial is going to show you how to read a specific line from a text file in Python using two different ways.
In the first example, we will see how to perform this task using the Python readlines()
function. In our second example, we will use the linecache
Python module for reading a specific line.
In my previous Python tutorials, I have shown you several things you can do with a text file like
Read a specific line from a text file in Python
Using readlines()
method
Using the readlines()
is the easiest way to read a specific line from a text file in Python. Below is given the code snippet:
file_variable = open('filename.txt') all_lines_variable = file_variable.readlines() print(all_lines_variable[specific_line_number - 1])
- Where
file_variable
is the variable name. all_lines_variable
– This is the variable to hold the lines as an Objectall_lines[specific_line_number - 1]
– If you want to read the line no 15 then put 14 in the square brackets.
Assume that we have a text file with the file nameĀ this_is_file.txt
Here is the content of the text file:
I am line no 1 I am line no 2 I am line no 3 I am line no 4 I am line no 5 I am line no 6 I am line no 7 I am line no 8 I am line no 9 I am line no 10
Now we have to read the texts from line no 5.
Then we will use the below code:
file = open('this_is_file.txt') all_lines = file.readlines() print(all_lines[4])
Output:
I am line no 5
Using the linecache Python module
I love Python just because of its cool built-in modules.
Thankfully Python has linecache module which allows us to get any particular line from any file.
Let’s see an example…
Suppose you are told to read line no 7 from the text file.
Here is the code:
import linecache print(linecache.getline('this_is_file.txt',7))
Output:
I am line no 7
To know more about the linecache module see theĀ linecache documentation
Thank you so much!
We are glad that it helped you
Hello. Can you tell me how to read a text file starting from a specific line?
Like, if it has line 1 to line 5, I want to start reading at line 2 onward till line 5 and ignore line 1.
You can use this “print(all_lines[i])” in a for loop where value of i will be increased from 2 to 5.
I learned sth but I’m sorry to say it does not help for large files like the fastText word embedding from facebook. The first method (read_lines) is a total deal breaker as it will need to hold all 2 million lines in memory and trust me when i say That no personal computer from 2011 (like my laptop) can handle.
I tried using linecache and unfortunately it does not seem to work for .Vec files. I mean it did not print the line but still it took 79.41100716590881 seconds to process my python code. It should’ve done anything. my python code tried to print 250000th line.
—————
for reference, i did this with a for loop and it took 8 secs to process that same thing and it also was able to print the line.
there’s more. it managed to print the 1900000th line in 79 secs. so we know that this method does not work for huge .vec files and if it does, it’s much slower than the simple for loop.
Any ideas on that?
I am not sure how to do that without storing the lines in memory. If you find a solution kindly put that here so that it helps other learners.
I want to search a specific line with the string name and change it..how can i do that?..
Like in a text file if there is a line Apple=Fruit…i want to search this line whereever it is in the file and change it to Apple = Tasty Fruit..how can i do that??
Please help me
Just find the substring in that line. with find() method then if the substring found then replace that with re.sub() function.
Am I able to set a variable as a specific line in another file? For example, if line 7 in a file said “hello” could I set a variable to what line 7 says so that if I were to for example print that variable, it would also say “hello”?
Yes, you can definitely do that. Just assign any variable to that specific line content as a string.
I want to know how can I find the line no. from a particular file. Like if my file as a list of names, I would like to know which specific line does a name appear.(P.S: This code was also really helpful!)
This is very useful for a beginner like me. Thank you so much,Keep writing more to help people like me.
Hi – how to count a given two or three values all found in the same line as one line. Example, five single sporty ladies. Values are five, single, ladies but will be counted just one 1 line. Thanks in advance.
This code always returned an empty line beneath my chosen line, how can i get rid of this?
Use the strip() method on the string. This will remove leading and trailing whitespace including the NewLine which is causing the empty line below.
Need to process lines that are in following format:
Apr 13 01:03:45 ip-172-31-26-135 haproxy[3883789]: 124.123.169.185:58813 [13/Apr/2021:01:01:44.417] ssl~ ws_demo/172.31.1.72 0/0/1/2/121389 101 533 – – —- 1/1/0/0/0 0/0 “GET /asr?dev=demo-api-2021-02-05-0001 HTTP/1.1”
does anyone know to do this in python?
Thank you, this code was really helpful to me