Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc Read a file line-by-line
in Python
Given a text file fname, a number N, the task is to read the last N lines of the file.
As we know, Python provides multiple in-built features and modules for handling files. Let’s discuss different ways to read last N lines of a file using Python.
File:
Bàn luận
In this approach, the idea is to use a negative iterator with the readlines[] function to read all the lines requested by the user from the end of file.
Python3
Điều kiện tiên quyết: Đọc từng dòng tệp trong Pythongiven một tệp văn bản fname, một số n, nhiệm vụ là đọc n dòng cuối cùng của tệp. . Hãy để thảo luận về các cách khác nhau để đọc n dòng cuối cùng của một tệp bằng python. & Nbsp; tệp: & nbsp; & nbsp;
with
open
[fname] as
file
Eighth line Ninth line Tenth line0
Phương pháp 1: Cách tiếp cận ngây thơ & nbsp; Trong phương pháp này, ý tưởng là sử dụng trình lặp âm với hàm readlines [] để đọc tất cả các dòng được người dùng yêu cầu từ cuối tệp. & Nbsp;
Eighth line Ninth line Tenth line0
Eighth line Ninth line Tenth line1
Eighth line Ninth line Tenth line2
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line4
def
LastNlines[fname, N]:
Eighth line Ninth line Tenth line1
Eighth line Ninth line Tenth line2
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line4
Eighth line Ninth line Tenth line5
file
Eighth line Ninth line Tenth line7
Eighth line Ninth line Tenth line8
Eighth line Ninth line Tenth line9
Eighth line Ninth line Tenth line5
Eighth line Ninth line Tenth line6
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line9____10
LastNlines[fname, N]:
0
Eighth line Ninth line Tenth line0
Eighth line Ninth line Tenth line1
LastNlines[fname, N]:
3
LastNlines[fname, N]:
5
Eighth line Ninth line Tenth line0
Eighth line Ninth line Tenth line1
Eighth line Ninth line Tenth line1
Eighth line Ninth line Tenth line5
0Output:
Eighth line Ninth line Tenth line
def
2
Eighth line Ninth line Tenth line3
def
4Method 2: Using OS module and buffering policy
In this approach, the idea is to work on the buffering policy in the python. A buffer stores a part of data received from a file stream of the operating system for a time period it is used and then more data comes in.
The buffer size determines the size of the data that can be stored at a time until it is used. We have the option to pass an integer to buffering in order to set buffering policy and if we do not specify any policy then the size of the buffer depends upon the device’s block size. Usually, the buffer is 4096 or 8192 bytes long. In this approach size of the buffer is 8192 bytes.
Moreover, the st_size attribute of os.stat[] method in the OS module is used to represent the size of the file in bytes.
Below is the implementation of the above approach.
Python3
def
6
Eighth line Ninth line Tenth line3
def
8Điều kiện tiên quyết: Đọc từng dòng tệp trong Pythongiven một tệp văn bản fname, một số n, nhiệm vụ là đọc n dòng cuối cùng của tệp. . Hãy để thảo luận về các cách khác nhau để đọc n dòng cuối cùng của một tệp bằng python. & Nbsp; tệp: & nbsp; & nbsp;
Phương pháp 1: Cách tiếp cận ngây thơ & nbsp; Trong phương pháp này, ý tưởng là sử dụng trình lặp âm với hàm readlines [] để đọc tất cả các dòng được người dùng yêu cầu từ cuối tệp. & Nbsp;
def
LastNlines[fname, N]:
Eighth line Ninth line Tenth line1
Eighth line Ninth line Tenth line2
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line4
Eighth line Ninth line Tenth line5
file
Eighth line Ninth line Tenth line7
Eighth line Ninth line Tenth line8
Eighth line Ninth line Tenth line9
with
open
open
0
Eighth line Ninth line Tenth line5
Eighth line Ninth line Tenth line6
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line9____10
def
2
Eighth line Ninth line Tenth line3
def
4
def
6
Eighth line Ninth line Tenth line3
def
8& nbsp; Phương pháp 2: Sử dụng mô -đun HĐH và chính sách đệm & nbsp; Trong phương pháp này, ý tưởng là thực hiện chính sách đệm trong Python. Một bộ đệm lưu trữ một phần dữ liệu nhận được từ luồng tệp của hệ điều hành trong một khoảng thời gian nó được sử dụng và sau đó nhiều dữ liệu đi vào. & NBSP; Kích thước bộ đệm xác định kích thước của dữ liệu có thể được lưu trữ tại một thời điểm cho đến khi nó Được sử dụng. Chúng tôi có tùy chọn chuyển một số nguyên cho bộ đệm để đặt chính sách đệm và nếu chúng tôi không chỉ định bất kỳ chính sách nào thì kích thước của bộ đệm phụ thuộc vào kích thước khối của thiết bị. Thông thường, bộ đệm dài 4096 hoặc 8192 byte. Trong phương pháp này, kích thước của bộ đệm là 8192 byte.MoreOver, thuộc tính st_size của phương thức Os.stat [] trong mô -đun HĐH được sử dụng để biểu diễn kích thước của tệp trong byte.Below là việc triển khai phương pháp trên & nbsp;
1
2
6
Eighth line Ninth line Tenth line3
8[fname] as
8
Eighth line Ninth line Tenth line01
with
0
Eighth line Ninth line Tenth line3
with
2
with
4
Eighth line Ninth line Tenth line3
with
6Eighth line Ninth line Tenth line14
Eighth line Ninth line Tenth line20
def
LastNlines[fname, N]:
Eighth line Ninth line Tenth line1
Eighth line Ninth line Tenth line2
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line4
Eighth line Ninth line Tenth line5
file
Eighth line Ninth line Tenth line7
Eighth line Ninth line Tenth line8
Eighth line Ninth line Tenth line9
Eighth line Ninth line Tenth line5
Eighth line Ninth line Tenth line6
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line9____10
LastNlines[fname, N]:
0
Eighth line Ninth line Tenth line0
Eighth line Ninth line Tenth line1
LastNlines[fname, N]:
3
LastNlines[fname, N]:
5
Eighth line Ninth line Tenth line0
Eighth line Ninth line Tenth line1
Eighth line Ninth line Tenth line1
Eighth line Ninth line Tenth line5
0file
9Output:
Eighth line Ninth line Tenth line
def
2
Eighth line Ninth line Tenth line3
def
4Method 3: Through Exponential search
In this method, the idea is to use Exponential Search algorithm which is generally used for searching sorted, unbounded or infinite lists. To get information about exponential search click here.
This approach uses assert statement which acts as a debugging tool to checks a condition. The program will continue to execute if the given statement is true otherwise, it generates an AssertionError exception. To get more details of assert statements click here.
Click here to get familiar with different kinds of use of seek[] method.
Below is the implementation of the above approach.
Python3
Điều kiện tiên quyết: Đọc từng dòng tệp trong Pythongiven một tệp văn bản fname, một số n, nhiệm vụ là đọc n dòng cuối cùng của tệp. . Hãy để thảo luận về các cách khác nhau để đọc n dòng cuối cùng của một tệp bằng python. & Nbsp; tệp: & nbsp; & nbsp;
Phương pháp 1: Cách tiếp cận ngây thơ & nbsp; Trong phương pháp này, ý tưởng là sử dụng trình lặp âm với hàm readlines [] để đọc tất cả các dòng được người dùng yêu cầu từ cuối tệp. & Nbsp;
def
LastNlines[fname, N]:
Eighth line Ninth line Tenth line1
Eighth line Ninth line Tenth line2
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line4
Eighth line Ninth line Tenth line5
file
Eighth line Ninth line Tenth line7
Eighth line Ninth line Tenth line8
Eighth line Ninth line Tenth line9
with
open
open
0
Eighth line Ninth line Tenth line5
Eighth line Ninth line Tenth line6
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line9____10
Eighth line Ninth line Tenth line0
LastNlines[fname, N]:
0Eighth line Ninth line Tenth line0
[fname] as
8
Eighth line Ninth line Tenth line79
Eighth line Ninth line Tenth line8
Eighth line Ninth line Tenth line81
Eighth line Ninth line Tenth line82
file
9
def
2
Eighth line Ninth line Tenth line3
def
4[fname] as
8
Eighth line Ninth line Tenth line79
with
6file
9[fname] as
8
Eighth line Ninth line Tenth line20
Eighth line Ninth line Tenth line0
Eighth line Ninth line Tenth line94
Eighth line Ninth line Tenth line0
def
6
Eighth line Ninth line Tenth line3
def
8& nbsp; Phương pháp 2: Sử dụng mô -đun HĐH và chính sách đệm & nbsp; Trong phương pháp này, ý tưởng là thực hiện chính sách đệm trong Python. Một bộ đệm lưu trữ một phần dữ liệu nhận được từ luồng tệp của hệ điều hành trong một khoảng thời gian nó được sử dụng và sau đó nhiều dữ liệu đi vào. & NBSP; Kích thước bộ đệm xác định kích thước của dữ liệu có thể được lưu trữ tại một thời điểm cho đến khi nó Được sử dụng. Chúng tôi có tùy chọn chuyển một số nguyên cho bộ đệm để đặt chính sách đệm và nếu chúng tôi không chỉ định bất kỳ chính sách nào thì kích thước của bộ đệm phụ thuộc vào kích thước khối của thiết bị. Thông thường, bộ đệm dài 4096 hoặc 8192 byte. Trong phương pháp này, kích thước của bộ đệm là 8192 byte.MoreOver, thuộc tính st_size của phương thức Os.stat [] trong mô -đun HĐH được sử dụng để biểu diễn kích thước của tệp trong byte.Below là việc triển khai phương pháp trên & nbsp;
1
2
def
LastNlines[fname, N]:
Eighth line Ninth line Tenth line1
Eighth line Ninth line Tenth line2
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line4
Eighth line Ninth line Tenth line5
file
Eighth line Ninth line Tenth line7
Eighth line Ninth line Tenth line8
Eighth line Ninth line Tenth line9
Eighth line Ninth line Tenth line5
Eighth line Ninth line Tenth line6
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line9____10
LastNlines[fname, N]:
0
Eighth line Ninth line Tenth line0
def
2
Eighth line Ninth line Tenth line3
def
4
def
6
Eighth line Ninth line Tenth line3
def
8Eighth line Ninth line Tenth line0____21
Eighth line Ninth line Tenth line2
Eighth line Ninth line Tenth line3
Eighth line Ninth line Tenth line4
LastNlines[fname, N]:
5
Eighth line Ninth line Tenth line0
Eighth line Ninth line Tenth line1
Eighth line Ninth line Tenth line1
Eighth line Ninth line Tenth line5
0file
9Output:
Eighth line Ninth line Tenth line