Trong khi vòng lặp đọc tệp Python

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()

  1. Đọc tệp ở chế độ văn bản. Nó trả về một luồng tới tệp
  2. Tạo một vòng lặp While vô hạn
    1. 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()
    2. 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
  3. 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
  4. 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ệp

Trong 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ược

Trong 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.
51

Hi User!
Welcome to Python Examples.
Continue Exploring.
52

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Hi User!
Welcome to Python Examples.
Continue Exploring.
54
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Line1 Geeks
Line2 for
Line3 Geeks
0
Line1 Geeks
Line2 for
Line3 Geeks
1
Line1 Geeks
Line2 for
Line3 Geeks
2
Line1 Geeks
Line2 for
Line3 Geeks
3
Line1 Geeks
Line2 for
Line3 Geeks
2
Line1 Geeks
Line2 for
Line3 Geeks
1
Line1 Geeks
Line2 for
Line3 Geeks
6

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Line1 Geeks
Line2 for
Line3 Geeks
8

Line1 Geeks
Line2 for
Line3 Geeks
9
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
51
Hi User!
Welcome to Python Examples.
Continue Exploring.
52
Hi User!
Welcome to Python Examples.
Continue Exploring.
53
Line1 Geeks
Line2 for
Line3 Geeks
2
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
56

Hi User!
Welcome to Python Examples.
Continue Exploring.
57

Hi User!
Welcome to Python Examples.
Continue Exploring.
58

Hi 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
30

Line1 Geeks
Line2 for
Line3 Geeks
9
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
51
Hi User!
Welcome to Python Examples.
Continue Exploring.
52
Hi User!
Welcome to Python Examples.
Continue Exploring.
53
Line1 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
37
Hi 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
39
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
21

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

________ 323 ________ 155 ________ 325

Hi User!
Welcome to Python Examples.
Continue Exploring.
26

Hi User!
Welcome to Python Examples.
Continue Exploring.
27
Hi User!
Welcome to Python Examples.
Continue Exploring.
28
Hi User!
Welcome to Python Examples.
Continue Exploring.
29
Hi User!
Welcome to Python Examples.
Continue Exploring.
510

Hi User!
Welcome to Python Examples.
Continue Exploring.
511
Hi User!
Welcome to Python Examples.
Continue Exploring.
23____1513
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
515

Hi User!
Welcome to Python Examples.
Continue Exploring.
511
Hi User!
Welcome to Python Examples.
Continue Exploring.
517
Hi User!
Welcome to Python Examples.
Continue Exploring.
52
Hi User!
Welcome to Python Examples.
Continue Exploring.
519
Hi User!
Welcome to Python Examples.
Continue Exploring.
520
Hi User!
Welcome to Python Examples.
Continue Exploring.
521
Hi 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
1

Phươ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

Trong khi vòng lặp đọc tệp Python

Ví dụ

Python3




Hi User!
Welcome to Python Examples.
Continue Exploring.
523

Hi User!
Welcome to Python Examples.
Continue Exploring.
524

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Hi User!
Welcome to Python Examples.
Continue Exploring.
54
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Line1 Geeks
Line2 for
Line3 Geeks
0
Line1 Geeks
Line2 for
Line3 Geeks
1
Line1 Geeks
Line2 for
Line3 Geeks
2
Line1 Geeks
Line2 for
Line3 Geeks
3
Line1 Geeks
Line2 for
Line3 Geeks
2
Line1 Geeks
Line2 for
Line3 Geeks
1
Line1 Geeks
Line2 for
Line3 Geeks
6

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Hi User!
Welcome to Python Examples.
Continue Exploring.
536

Line1 Geeks
Line2 for
Line3 Geeks
9
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
51
Hi User!
Welcome to Python Examples.
Continue Exploring.
52
Hi User!
Welcome to Python Examples.
Continue Exploring.
53
Line1 Geeks
Line2 for
Line3 Geeks
2
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
56

Hi User!
Welcome to Python Examples.
Continue Exploring.
545

Hi User!
Welcome to Python Examples.
Continue Exploring.
58

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Hi User!
Welcome to Python Examples.
Continue Exploring.
548

Line1 Geeks
Line2 for
Line3 Geeks
9
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
51
Hi User!
Welcome to Python Examples.
Continue Exploring.
52
Hi User!
Welcome to Python Examples.
Continue Exploring.
53
Line1 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
37
Hi 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.
511
Hi User!
Welcome to Python Examples.
Continue Exploring.
23____1513
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
515

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Hi User!
Welcome to Python Examples.
Continue Exploring.
511
Line1 Geeks
Line2 for
Line3 Geeks
11

Hi User!
Welcome to Python Examples.
Continue Exploring.
511
Hi User!
Welcome to Python Examples.
Continue Exploring.
28
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Line1 Geeks
Line2 for
Line3 Geeks
15

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Hi User!
Welcome to Python Examples.
Continue Exploring.
511
Line1 Geeks
Line2 for
Line3 Geeks
18

Hi User!
Welcome to Python Examples.
Continue Exploring.
511
Line1 Geeks
Line2 for
Line3 Geeks
20

Hi User!
Welcome to Python Examples.
Continue Exploring.
511
Line1 Geeks
Line2 for
Line3 Geeks
22
Line1 Geeks
Line2 for
Line3 Geeks
23
Line1 Geeks
Line2 for
Line3 Geeks
24

Line1 Geeks
Line2 for
Line3 Geeks
25
Line1 Geeks
Line2 for
Line3 Geeks
26

Hi User!
Welcome to Python Examples.
Continue Exploring.
511
Hi User!
Welcome to Python Examples.
Continue Exploring.
517
Hi User!
Welcome to Python Examples.
Continue Exploring.
52
Hi User!
Welcome to Python Examples.
Continue Exploring.
519
Hi User!
Welcome to Python Examples.
Continue Exploring.
520
Hi User!
Welcome to Python Examples.
Continue Exploring.
521
Hi User!
Welcome to Python Examples.
Continue Exploring.
522

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Hi 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 for

Mộ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.
523

Line1 Geeks
Line2 for
Line3 Geeks
37

Line1 Geeks
Line2 for
Line3 Geeks
38

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Hi User!
Welcome to Python Examples.
Continue Exploring.
54
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Line1 Geeks
Line2 for
Line3 Geeks
0
Line1 Geeks
Line2 for
Line3 Geeks
1
Line1 Geeks
Line2 for
Line3 Geeks
2
Line1 Geeks
Line2 for
Line3 Geeks
3
Line1 Geeks
Line2 for
Line3 Geeks
2
Line1 Geeks
Line2 for
Line3 Geeks
1
Line1 Geeks
Line2 for
Line3 Geeks
6

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Line1 Geeks
Line2 for
Line3 Geeks
50

Line1 Geeks
Line2 for
Line3 Geeks
9
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
51
Hi User!
Welcome to Python Examples.
Continue Exploring.
52
Hi User!
Welcome to Python Examples.
Continue Exploring.
53
Line1 Geeks
Line2 for
Line3 Geeks
2
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
56

Hi User!
Welcome to Python Examples.
Continue Exploring.
57

Hi User!
Welcome to Python Examples.
Continue Exploring.
58

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Line1 Geeks
Line2 for
Line3 Geeks
62

Line1 Geeks
Line2 for
Line3 Geeks
9
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
51
Hi User!
Welcome to Python Examples.
Continue Exploring.
52
Hi User!
Welcome to Python Examples.
Continue Exploring.
53
Line1 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
37
Hi User!
Welcome to Python Examples.
Continue Exploring.
56

________ 323 ________ 155 ________ 325

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Line1 Geeks
Line2 for
Line3 Geeks
75

Hi User!
Welcome to Python Examples.
Continue Exploring.
517
Hi User!
Welcome to Python Examples.
Continue Exploring.
52____678
Hi User!
Welcome to Python Examples.
Continue Exploring.
56

Hi User!
Welcome to Python Examples.
Continue Exploring.
27
Hi User!
Welcome to Python Examples.
Continue Exploring.
28
Hi User!
Welcome to Python Examples.
Continue Exploring.
29
Line1 Geeks
Line2 for
Line3 Geeks
83

Hi User!
Welcome to Python Examples.
Continue Exploring.
511
Hi User!
Welcome to Python Examples.
Continue Exploring.
23____1513
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
515

Hi User!
Welcome to Python Examples.
Continue Exploring.
511
Hi User!
Welcome to Python Examples.
Continue Exploring.
517
Hi User!
Welcome to Python Examples.
Continue Exploring.
52
Hi User!
Welcome to Python Examples.
Continue Exploring.
519
Hi User!
Welcome to Python Examples.
Continue Exploring.
520
Hi User!
Welcome to Python Examples.
Continue Exploring.
521
Hi User!
Welcome to Python Examples.
Continue Exploring.
522

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Line1 Geeks
Line2 for
Line3 Geeks
97

Hi User!
Welcome to Python Examples.
Continue Exploring.
58

đầu ra

Hi User!
Welcome to Python Examples.
Continue Exploring.
5

Phươ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ách

Việ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
99
Hi User!
Welcome to Python Examples.
Continue Exploring.
51____152
Hi User!
Welcome to Python Examples.
Continue Exploring.
53
Hi User!
Welcome to Python Examples.
Continue Exploring.
503

Hi User!
Welcome to Python Examples.
Continue Exploring.
511
Hi User!
Welcome to Python Examples.
Continue Exploring.
505
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
507
Hi User!
Welcome to Python Examples.
Continue Exploring.
27
Hi User!
Welcome to Python Examples.
Continue Exploring.
28
Hi User!
Welcome to Python Examples.
Continue Exploring.
29
Hi User!
Welcome to Python Examples.
Continue Exploring.
511

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Hi User!
Welcome to Python Examples.
Continue Exploring.
517
Hi User!
Welcome to Python Examples.
Continue Exploring.
514

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Hi User!
Welcome to Python Examples.
Continue Exploring.
516

Line1 Geeks
Line2 for
Line3 Geeks
99
Hi User!
Welcome to Python Examples.
Continue Exploring.
51____152
Hi User!
Welcome to Python Examples.
Continue Exploring.
53
Hi User!
Welcome to Python Examples.
Continue Exploring.
503

Hi User!
Welcome to Python Examples.
Continue Exploring.
511
Hi User!
Welcome to Python Examples.
Continue Exploring.
505
Hi User!
Welcome to Python Examples.
Continue Exploring.
55
Hi User!
Welcome to Python Examples.
Continue Exploring.
525
Hi User!
Welcome to Python Examples.
Continue Exploring.
27
Hi User!
Welcome to Python Examples.
Continue Exploring.
28
Hi User!
Welcome to Python Examples.
Continue Exploring.
29
Hi User!
Welcome to Python Examples.
Continue Exploring.
511

Hi User!
Welcome to Python Examples.
Continue Exploring.
53

Hi User!
Welcome to Python Examples.
Continue Exploring.
517
Hi 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
3

Vớ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.