Có nhiều cách để đọc từng dòng một tệp văn bản trong Python. Bạn có thể đọc các dòng trong danh sách hoặc chỉ truy cập từng dòng trong một vòng lặp bằng cách lặp qua các dòng được cung cấp bởi một số loại trình vòng lặp hoặc gọi một hàm trên đối tượng tệp Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách đọc từng dòng tệp bằng cách sử dụng hàm readline(), hàm readlines() hoặc đối tượng tệp với sự trợ giúp của các chương trình ví dụ ví dụ 1. Đọc từng dòng tệp văn bản – readline()Trong ví dụ này, chúng tôi sẽ sử dụng hàm readline() trên luồng tệp để nhận dòng tiếp theo trong một vòng lặp Các bước sử dụng tệp. hàm readline() Sau đây là các bước để đọc từng dòng tệp bằng hàm readline() - Đọc tệp ở chế độ văn bản. Nó trả về một luồng tới tệp
- Tạo một vòng lặp While vô hạn
- Trong mỗi lần lặp của vòng lặp, hãy đọc dòng tiếp theo từ tệp bằng readline()
- Nếu dòng không trống, bạn có dòng tiếp theo. Bạn có thể kiểm tra điều này bằng cách sử dụng if-not. Nếu không, không còn dòng nào trong tệp và chúng tôi phá vỡ vòng lặp
- Vào thời điểm chúng tôi thoát ra khỏi vòng lặp, chúng tôi đã đọc từng dòng tệp một trong các lần lặp
- Vì chúng tôi đã hoàn thành với tệp, chúng tôi sẽ đóng tệp
Chương trình Python #get file object
file1 = open("sample.txt", "r")
while(True):
#read next line
line = file1.readline()
#check if line is not null
if not line:
break
#you can access the line
print(line.strip())
#close file
file1.close
đầu ra Hi User!
Welcome to Python Examples.
Continue Exploring.
ví dụ 2. Đọc các dòng dưới dạng danh sách – readlines()hàm readlines() trả về tất cả các dòng trong tệp dưới dạng danh sách các chuỗi. Chúng tôi có thể duyệt qua danh sách và truy cập từng dòng của tệp Trong chương trình sau, chúng ta sẽ đọc một tệp văn bản, sau đó lấy danh sách tất cả các dòng trong tệp văn bản bằng hàm readlines(). Sau đó, chúng tôi sử dụng Vòng lặp để duyệt qua danh sách các chuỗi này Chương trình Python #get file object
file1 = open("sample.txt", "r")
#read all lines
lines = file1.readlines()
#traverse through lines one by one
for line in lines:
print(line.strip())
#close file
file1.close
đầu ra Hi User!
Welcome to Python Examples.
Continue Exploring.
ví dụ 3. Đọc từng dòng tệp bằng cách sử dụng đối tượng tệpTrong ví dụ đầu tiên của chúng tôi, chúng tôi đã đọc từng dòng tệp bằng cách sử dụng vòng lặp while vô hạn và hàm readline(). Tuy nhiên, bạn có thể sử dụng câu lệnh For Loop trên chính đối tượng tệp để lấy dòng tiếp theo trong tệp trong mỗi lần lặp, cho đến khi kết thúc tệp Sau đây là chương trình minh họa cách chúng ta sử dụng câu lệnh for-in để lặp qua các dòng trong tệp Chương trình Python #get file object
file1 = open("sample.txt", "r")
#traverse through lines one by one
for line in file1:
print(line.strip())
#close file
file1.close
đầu ra Hi User!
Welcome to Python Examples.
Continue Exploring.
Tóm lượcTrong hướng dẫn về Ví dụ Python này, chúng ta đã học cách đọc từng dòng một tệp văn bản, với sự trợ giúp của các chương trình ví dụ Python rất chi tiết Python cung cấp các hàm sẵn có để tạo, viết và đọc tệp. Có hai loại tệp có thể được xử lý trong python, tệp văn bản bình thường và tệp nhị phân (được viết bằng ngôn ngữ nhị phân, 0 và 1). Trong bài này, chúng ta sẽ nghiên cứu cách đọc từng dòng từ một tập tin Phương pháp 1. Đọc từng dòng tệp bằng cách sử dụng readlines()readlines() được sử dụng để đọc tất cả các dòng trong một lần và sau đó trả về chúng dưới dạng mỗi dòng một phần tử chuỗi trong danh sách. Chức năng này có thể được sử dụng cho các tệp nhỏ, vì nó đọc toàn bộ nội dung tệp vào bộ nhớ, sau đó chia thành các dòng riêng biệt. Chúng ta có thể lặp lại danh sách và loại bỏ ký tự '\n' của dòng mới bằng cách sử dụng hàm strip() Ví dụ. Python3
Hi User!
Welcome to Python Examples.
Continue Exploring. 51Hi User!
Welcome to Python Examples.
Continue Exploring. 52Hi User!
Welcome to Python Examples.
Continue Exploring. 53Hi User!
Welcome to Python Examples.
Continue Exploring. 54Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Line1 Geeks
Line2 for
Line3 Geeks 0Line1 Geeks
Line2 for
Line3 Geeks 1Line1 Geeks
Line2 for
Line3 Geeks 2Line1 Geeks
Line2 for
Line3 Geeks 3Line1 Geeks
Line2 for
Line3 Geeks 2Line1 Geeks
Line2 for
Line3 Geeks 1Line1 Geeks
Line2 for
Line3 Geeks 6Hi User!
Welcome to Python Examples.
Continue Exploring. 53Line1 Geeks
Line2 for
Line3 Geeks 8Line1 Geeks
Line2 for
Line3 Geeks 9Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Hi User!
Welcome to Python Examples.
Continue Exploring. 51Hi User!
Welcome to Python Examples.
Continue Exploring. 52Hi User!
Welcome to Python Examples.
Continue Exploring. 53Line1 Geeks
Line2 for
Line3 Geeks 2Hi User!
Welcome to Python Examples.
Continue Exploring. 55Hi User!
Welcome to Python Examples.
Continue Exploring. 56Hi User!
Welcome to Python Examples.
Continue Exploring. 57Hi User!
Welcome to Python Examples.
Continue Exploring. 58Hi User!
Welcome to Python Examples.
Continue Exploring. 53#get file object
file1 = open("sample.txt", "r")
#read all lines
lines = file1.readlines()
#traverse through lines one by one
for line in lines:
print(line.strip())
#close file
file1.close 30Line1 Geeks
Line2 for
Line3 Geeks 9Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Hi User!
Welcome to Python Examples.
Continue Exploring. 51Hi User!
Welcome to Python Examples.
Continue Exploring. 52Hi User!
Welcome to Python Examples.
Continue Exploring. 53Line1 Geeks
Line2 for
Line3 Geeks 2#get file object
file1 = open("sample.txt", "r")
#read all lines
lines = file1.readlines()
#traverse through lines one by one
for line in lines:
print(line.strip())
#close file
file1.close 37Hi User!
Welcome to Python Examples.
Continue Exploring. 56#get file object
file1 = open("sample.txt", "r")
#read all lines
lines = file1.readlines()
#traverse through lines one by one
for line in lines:
print(line.strip())
#close file
file1.close 39Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Hi User!
Welcome to Python Examples.
Continue Exploring. 21Hi User!
Welcome to Python Examples.
Continue Exploring. 53________ 323 ________ 155 ________ 325 Hi User!
Welcome to Python Examples.
Continue Exploring. 26Hi User!
Welcome to Python Examples.
Continue Exploring. 27 Hi User!
Welcome to Python Examples.
Continue Exploring. 28Hi User!
Welcome to Python Examples.
Continue Exploring. 29 Hi User!
Welcome to Python Examples.
Continue Exploring. 510Hi User!
Welcome to Python Examples.
Continue Exploring. 511Hi User!
Welcome to Python Examples.
Continue Exploring. 23____1513Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Hi User!
Welcome to Python Examples.
Continue Exploring. 515Hi User!
Welcome to Python Examples.
Continue Exploring. 511Hi User!
Welcome to Python Examples.
Continue Exploring. 517Hi User!
Welcome to Python Examples.
Continue Exploring. 52Hi User!
Welcome to Python Examples.
Continue Exploring. 519Hi User!
Welcome to Python Examples.
Continue Exploring. 520Hi User!
Welcome to Python Examples.
Continue Exploring. 521Hi User!
Welcome to Python Examples.
Continue Exploring. 522Đầu ra. #get file object
file1 = open("sample.txt", "r")
#traverse through lines one by one
for line in file1:
print(line.strip())
#close file
file1.close 1Phương pháp 2. Đọc từng dòng tệp bằng cách sử dụng readline()hàm readline() đọc một dòng của tệp và trả về dưới dạng chuỗi. Nó nhận một tham số n, xác định số byte tối đa sẽ được đọc. Tuy nhiên, không đọc nhiều hơn một dòng, ngay cả khi n vượt quá độ dài của dòng. Nó sẽ hiệu quả khi đọc một tệp lớn vì thay vì tìm nạp tất cả dữ liệu trong một lần, nó sẽ tìm nạp từng dòng. readline() trả về dòng tiếp theo của tệp chứa ký tự xuống dòng ở cuối. Ngoài ra, nếu đến cuối tệp, nó sẽ trả về một chuỗi rỗng Ví dụ Python3
Hi User!
Welcome to Python Examples.
Continue Exploring. 523Hi User!
Welcome to Python Examples.
Continue Exploring. 524Hi User!
Welcome to Python Examples.
Continue Exploring. 53Hi User!
Welcome to Python Examples.
Continue Exploring. 54Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Line1 Geeks
Line2 for
Line3 Geeks 0Line1 Geeks
Line2 for
Line3 Geeks 1Line1 Geeks
Line2 for
Line3 Geeks 2Line1 Geeks
Line2 for
Line3 Geeks 3Line1 Geeks
Line2 for
Line3 Geeks 2Line1 Geeks
Line2 for
Line3 Geeks 1Line1 Geeks
Line2 for
Line3 Geeks 6Hi User!
Welcome to Python Examples.
Continue Exploring. 53Hi User!
Welcome to Python Examples.
Continue Exploring. 536Line1 Geeks
Line2 for
Line3 Geeks 9Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Hi User!
Welcome to Python Examples.
Continue Exploring. 51Hi User!
Welcome to Python Examples.
Continue Exploring. 52Hi User!
Welcome to Python Examples.
Continue Exploring. 53Line1 Geeks
Line2 for
Line3 Geeks 2Hi User!
Welcome to Python Examples.
Continue Exploring. 55Hi User!
Welcome to Python Examples.
Continue Exploring. 56Hi User!
Welcome to Python Examples.
Continue Exploring. 545Hi User!
Welcome to Python Examples.
Continue Exploring. 58Hi User!
Welcome to Python Examples.
Continue Exploring. 53Hi User!
Welcome to Python Examples.
Continue Exploring. 548Line1 Geeks
Line2 for
Line3 Geeks 9Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Hi User!
Welcome to Python Examples.
Continue Exploring. 51Hi User!
Welcome to Python Examples.
Continue Exploring. 52Hi User!
Welcome to Python Examples.
Continue Exploring. 53Line1 Geeks
Line2 for
Line3 Geeks 2#get file object
file1 = open("sample.txt", "r")
#read all lines
lines = file1.readlines()
#traverse through lines one by one
for line in lines:
print(line.strip())
#close file
file1.close 37Hi User!
Welcome to Python Examples.
Continue Exploring. 56________ 323 ________ 155 ________ 325 Hi User!
Welcome to Python Examples.
Continue Exploring. 53________ 601 ________ 602 ________ 603 Hi User!
Welcome to Python Examples.
Continue Exploring. 511Hi User!
Welcome to Python Examples.
Continue Exploring. 23____1513Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Hi User!
Welcome to Python Examples.
Continue Exploring. 515Hi User!
Welcome to Python Examples.
Continue Exploring. 53Hi User!
Welcome to Python Examples.
Continue Exploring. 511Line1 Geeks
Line2 for
Line3 Geeks 11Hi User!
Welcome to Python Examples.
Continue Exploring. 511Hi User!
Welcome to Python Examples.
Continue Exploring. 28Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Line1 Geeks
Line2 for
Line3 Geeks 15Hi User!
Welcome to Python Examples.
Continue Exploring. 53Hi User!
Welcome to Python Examples.
Continue Exploring. 511Line1 Geeks
Line2 for
Line3 Geeks 18Hi User!
Welcome to Python Examples.
Continue Exploring. 511Line1 Geeks
Line2 for
Line3 Geeks 20Hi User!
Welcome to Python Examples.
Continue Exploring. 511Line1 Geeks
Line2 for
Line3 Geeks 22 Line1 Geeks
Line2 for
Line3 Geeks 23 Line1 Geeks
Line2 for
Line3 Geeks 24Line1 Geeks
Line2 for
Line3 Geeks 25Line1 Geeks
Line2 for
Line3 Geeks 26Hi User!
Welcome to Python Examples.
Continue Exploring. 511Hi User!
Welcome to Python Examples.
Continue Exploring. 517Hi User!
Welcome to Python Examples.
Continue Exploring. 52Hi User!
Welcome to Python Examples.
Continue Exploring. 519Hi User!
Welcome to Python Examples.
Continue Exploring. 520Hi User!
Welcome to Python Examples.
Continue Exploring. 521Hi User!
Welcome to Python Examples.
Continue Exploring. 522Hi User!
Welcome to Python Examples.
Continue Exploring. 53Hi User!
Welcome to Python Examples.
Continue Exploring. 58đầu ra. Line1 Geeks
Line2 for
Line3 Geeks Phương pháp 3. Đọc từng dòng tệp bằng cách sử dụng vòng lặp forMột đối tượng có thể lặp lại được trả về bởi hàm open() trong khi mở tệp. Cách cuối cùng này để đọc tệp theo từng dòng bao gồm việc lặp qua một đối tượng tệp trong một vòng lặp. Khi làm điều này, chúng ta đang tận dụng lợi thế của hàm Python tích hợp cho phép chúng ta lặp lại hoàn toàn đối tượng tệp bằng cách sử dụng vòng lặp for kết hợp với việc sử dụng đối tượng có thể lặp lại. Cách tiếp cận này cần ít dòng mã hơn, đây luôn là cách thực hành tốt nhất đáng làm theo Ví dụ Python3
Hi User!
Welcome to Python Examples.
Continue Exploring. 523Line1 Geeks
Line2 for
Line3 Geeks 37Line1 Geeks
Line2 for
Line3 Geeks 38Hi User!
Welcome to Python Examples.
Continue Exploring. 53Hi User!
Welcome to Python Examples.
Continue Exploring. 54Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Line1 Geeks
Line2 for
Line3 Geeks 0Line1 Geeks
Line2 for
Line3 Geeks 1Line1 Geeks
Line2 for
Line3 Geeks 2Line1 Geeks
Line2 for
Line3 Geeks 3Line1 Geeks
Line2 for
Line3 Geeks 2Line1 Geeks
Line2 for
Line3 Geeks 1Line1 Geeks
Line2 for
Line3 Geeks 6Hi User!
Welcome to Python Examples.
Continue Exploring. 53Line1 Geeks
Line2 for
Line3 Geeks 50Line1 Geeks
Line2 for
Line3 Geeks 9Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Hi User!
Welcome to Python Examples.
Continue Exploring. 51Hi User!
Welcome to Python Examples.
Continue Exploring. 52Hi User!
Welcome to Python Examples.
Continue Exploring. 53Line1 Geeks
Line2 for
Line3 Geeks 2Hi User!
Welcome to Python Examples.
Continue Exploring. 55Hi User!
Welcome to Python Examples.
Continue Exploring. 56Hi User!
Welcome to Python Examples.
Continue Exploring. 57Hi User!
Welcome to Python Examples.
Continue Exploring. 58Hi User!
Welcome to Python Examples.
Continue Exploring. 53Line1 Geeks
Line2 for
Line3 Geeks 62Line1 Geeks
Line2 for
Line3 Geeks 9Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Hi User!
Welcome to Python Examples.
Continue Exploring. 51Hi User!
Welcome to Python Examples.
Continue Exploring. 52Hi User!
Welcome to Python Examples.
Continue Exploring. 53Line1 Geeks
Line2 for
Line3 Geeks 2#get file object
file1 = open("sample.txt", "r")
#read all lines
lines = file1.readlines()
#traverse through lines one by one
for line in lines:
print(line.strip())
#close file
file1.close 37Hi User!
Welcome to Python Examples.
Continue Exploring. 56________ 323 ________ 155 ________ 325 Hi User!
Welcome to Python Examples.
Continue Exploring. 53Line1 Geeks
Line2 for
Line3 Geeks 75Hi User!
Welcome to Python Examples.
Continue Exploring. 517Hi User!
Welcome to Python Examples.
Continue Exploring. 52____678Hi User!
Welcome to Python Examples.
Continue Exploring. 56Hi User!
Welcome to Python Examples.
Continue Exploring. 27 Hi User!
Welcome to Python Examples.
Continue Exploring. 28Hi User!
Welcome to Python Examples.
Continue Exploring. 29 Line1 Geeks
Line2 for
Line3 Geeks 83Hi User!
Welcome to Python Examples.
Continue Exploring. 511Hi User!
Welcome to Python Examples.
Continue Exploring. 23____1513Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Hi User!
Welcome to Python Examples.
Continue Exploring. 515Hi User!
Welcome to Python Examples.
Continue Exploring. 511Hi User!
Welcome to Python Examples.
Continue Exploring. 517Hi User!
Welcome to Python Examples.
Continue Exploring. 52Hi User!
Welcome to Python Examples.
Continue Exploring. 519Hi User!
Welcome to Python Examples.
Continue Exploring. 520Hi User!
Welcome to Python Examples.
Continue Exploring. 521Hi User!
Welcome to Python Examples.
Continue Exploring. 522Hi User!
Welcome to Python Examples.
Continue Exploring. 53Line1 Geeks
Line2 for
Line3 Geeks 97Hi User!
Welcome to Python Examples.
Continue Exploring. 58đầu ra Hi User!
Welcome to Python Examples.
Continue Exploring. 5Phương pháp 4. Đọc từng dòng tệp bằng cách sử dụng vòng lặp for và hiểu danh sáchViệc hiểu danh sách bao gồm các dấu ngoặc chứa biểu thức, được thực thi cho từng phần tử cùng với vòng lặp for để lặp qua từng phần tử. Ở đây, chúng tôi sẽ đọc tệp văn bản và in dữ liệu thô bao gồm ký tự dòng mới trong một đầu ra khác, chúng tôi đã xóa tất cả các ký tự dòng mới khỏi danh sách Ví dụ Python3
Line1 Geeks
Line2 for
Line3 Geeks 99Hi User!
Welcome to Python Examples.
Continue Exploring. 51____152Hi User!
Welcome to Python Examples.
Continue Exploring. 53Hi User!
Welcome to Python Examples.
Continue Exploring. 503Hi User!
Welcome to Python Examples.
Continue Exploring. 511Hi User!
Welcome to Python Examples.
Continue Exploring. 505Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Hi User!
Welcome to Python Examples.
Continue Exploring. 507Hi User!
Welcome to Python Examples.
Continue Exploring. 27 Hi User!
Welcome to Python Examples.
Continue Exploring. 28Hi User!
Welcome to Python Examples.
Continue Exploring. 29 Hi User!
Welcome to Python Examples.
Continue Exploring. 511Hi User!
Welcome to Python Examples.
Continue Exploring. 53Hi User!
Welcome to Python Examples.
Continue Exploring. 517Hi User!
Welcome to Python Examples.
Continue Exploring. 514Hi User!
Welcome to Python Examples.
Continue Exploring. 53Hi User!
Welcome to Python Examples.
Continue Exploring. 516Line1 Geeks
Line2 for
Line3 Geeks 99Hi User!
Welcome to Python Examples.
Continue Exploring. 51____152Hi User!
Welcome to Python Examples.
Continue Exploring. 53Hi User!
Welcome to Python Examples.
Continue Exploring. 503Hi User!
Welcome to Python Examples.
Continue Exploring. 511Hi User!
Welcome to Python Examples.
Continue Exploring. 505Hi User!
Welcome to Python Examples.
Continue Exploring. 55 Hi User!
Welcome to Python Examples.
Continue Exploring. 525Hi User!
Welcome to Python Examples.
Continue Exploring. 27 Hi User!
Welcome to Python Examples.
Continue Exploring. 28Hi User!
Welcome to Python Examples.
Continue Exploring. 29 Hi User!
Welcome to Python Examples.
Continue Exploring. 511Hi User!
Welcome to Python Examples.
Continue Exploring. 53Hi User!
Welcome to Python Examples.
Continue Exploring. 517Hi User!
Welcome to Python Examples.
Continue Exploring. 514đầu ra #get file object
file1 = open("sample.txt", "r")
#read all lines
lines = file1.readlines()
#traverse through lines one by one
for line in lines:
print(line.strip())
#close file
file1.close 3Với tuyên bốTrong các cách tiếp cận trên, mỗi khi tệp được mở, tệp cần được đóng một cách rõ ràng. Nếu một người quên đóng tệp, nó có thể gây ra một số lỗi trong mã, tôi. e. nhiều thay đổi trong tệp không có hiệu lực cho đến khi tệp được đóng đúng cách. Để ngăn chặn điều này với tuyên bố có thể được sử dụng. Câu lệnh With trong Python được sử dụng trong xử lý ngoại lệ để làm cho mã sạch hơn và dễ đọc hơn nhiều. Nó đơn giản hóa việc quản lý các tài nguyên phổ biến như luồng tệp. Quan sát ví dụ mã sau đây về cách sử dụng câu lệnh with làm cho mã sạch hơn. Không cần phải gọi tập tin. close() khi sử dụng với câu lệnh. Bản thân câu lệnh with đảm bảo việc thu thập và giải phóng tài nguyên phù hợp
Bạn có thể đọc từ một tệp bằng Python không?
Bạn có thể lập trình mã của mình để đọc dữ liệu hoặc hướng dẫn từ tệp rồi ghi cả dữ liệu. Điều này làm tăng hiệu quả và giảm nỗ lực thủ công. Python có một phương pháp được xác định rõ để mở, đọc và ghi tệp.
Làm cách nào để đọc tệp văn bản trong Python?
Nếu bạn muốn đọc một tệp văn bản bằng Python, trước tiên bạn phải mở tệp đó. Nếu tệp văn bản và tệp hiện tại của bạn nằm trong cùng một thư mục ("thư mục"), thì bạn chỉ cần tham chiếu tên tệp trong hàm open().
Bạn có thể lặp qua một tệp Python không?
Có, bạn có thể lặp qua phần xử lý tệp , không cần gọi hàm readlines(). Bằng cách này, trên các tệp lớn, bạn không cần phải đọc tất cả các dòng (đó là chức năng của readlines()) cùng một lúc. |