[Python] Read the specified line in the file

Read the specified line

A memo when you want to extract only the specified line from the file in the program

linecache Source code: https://hg.python.org/cpython/file/3.4/Lib/linecache.py Reference URL: http://docs.python.jp/3.4/library/linecache.html

The linecache module uses a cache (which typically reads many lines from a single file) to allow you to retrieve arbitrary lines in a Python source file with internal optimization. .. The traceback module makes use of this module to include the source code in a well-formed traceback.

You can read the specified line with linecache.getline (filename, lineno). Specify the file name in filename and the number of lines in lineno. lineno is an integer from1 ~. The sample.txt used below is a file that describes the Nth line and each line from the 0th line to the 1000th line. (When I first thought that lineno started with 0, it started with 1, so it started with 0.)

Code created this time: https://github.com/KodairaTomonori/Qiita/tree/master/default_module/linecache

sample

test_linecache.py


import linecache

a = input('How many lines do you want to retrieve? :')
target_line = linecache.getline('sample.txt', int(a))
print(target_line)
linecache.clearcache() 

output

$ head sample.txt 
Line 0
The first line
2nd line
3rd line
4th line
5th line
6th line
7th line
8th line
9th line
$ python test_linecache.py 
How many lines do you want to retrieve? : 100
Line 99

Commentary

linecache.getline returns the string for that specified line. The last linecache.clearcache () uses the cache, as mentioned in the first quoted sentence, so clear it when you no longer use the file.

If the file size is small, I think it is faster to make a list with readlines () normally. (Not a speedy story) Also, if you just want to check the specified line, head -100 sample.txt | tail -1 You can see it normally at.

Recommended Posts

[Python] Read the specified line in the file
Read the file line by line in Python
Download the file in Python
Save the binary file in Python
Various ways to read the last line of a csv file in Python
[Python] Open the csv file in the folder specified by pandas
Google search for the last line of the file in Python
Let's read the RINEX file with Python ①
Read the standard output of a subprocess line by line in Python
Read a file in Python with a relative path from the program
File operations in Python
Read DXF in python
File processing in Python
Read Python csv file
Read a file containing garbled lines in Python
Read table data in PDF file with Python
Read line by line from a file with Python
Test & Debug Tips: Create a file of the specified size in Python
Read the file with python and delete the line breaks [Notes on reading the file]
[Implementation example] Read the file line by line with Cython (Python) from the last line
Fizzbuzz in Python (in one line)
Find the difference in Python
How to read all the classes contained in * .py in the directory specified by Python
Python --Read data from a numeric data file and find the multiple regression line.
Read Euler's formula in Python
Read the csv file and display it in the browser
Read the xml file by referring to the Python tutorial
Try LINE Notify in Python
Read Outlook emails in Python
I want to get the file name, line number, and function name in Python 3.4
Read Fortran output in python
Output the specified table of Oracle database in Python to Excel for each file
How to switch the configuration file to be read by Python
Change the standard output destination to a file in Python
[Python] Read the csv file and display the figure with matplotlib
[Note] Import of a file in the parent directory in Python
After enabling the python virtual environment in the batch file, run the python file
[Python] Read command line arguments from file name or stdin
Output the line containing the specified string
Make python segfault in one line
Extract the xz file with python
Getting the arXiv API in Python
File / folder path manipulation in Python
Read Protocol Buffers data in Python3
Hit the Sesami API in Python
Try the Python LINE Pay SDK
Linebot creation & file sharing in Python
Get the desktop path in Python
Read PNG chunks in Python (class)
Get the script path in Python
In the python command python points to python3.8
Implement the Singleton pattern in Python
Create a binary file in Python
Type specified in python. Throw exceptions
The story of the "hole" in the file
Hit the web API in Python
Extract the targz file using python
Read files in parallel with Python
I wrote the queue in Python
Calculate the previous month in Python
Examine the object's class in python