Bạn có thể bắt chước thành ngữ C trong Python.
Để đọc một bộ đệm lên đến số
# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
1 byte, bạn có thể làm điều này:with open[filename, 'rb'] as f:
while True:
buf = f.read[max_size]
if not buf:
break
process[buf]
Hoặc, một dòng văn bản từng dòng:
# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
Bạn cần sử dụng cấu trúc
# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
2 vì không có bài kiểm tra EOF nào trong Python ngoài việc thiếu byte được trả về từ một lần đọc.Trong C, bạn có thể có:
while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
Tuy nhiên, bạn không thể có cái này trong Python:
while [line = f.readline[]]:
# syntax error
Bởi vì các bài tập không được phép trong các biểu thức trong Python [mặc dù các phiên bản gần đây của Python có thể bắt chước điều này bằng cách sử dụng các biểu thức gán, xem bên dưới].
Nó chắc chắn là thành ngữ hơn trong Python để làm điều này:
# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
Cập nhật: Vì Python 3.8, bạn cũng có thể sử dụng các biểu thức gán: Since Python 3.8 you may also use assignment expressions:
while line := f.readline[]:
process[line]
Điều đó hoạt động ngay cả khi dòng đọc là trống và tiếp tục cho đến khi EOF.
Prerequisites:
0____92while line := f.readline[]: process[line]
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']
3['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']
4
08# warning -- not idiomatic Python! See below... with open[filename, 'rb'] as f: while True: line = f.readline[] if not line: break process[line]
7# THIS IS IDIOMATIC Python. Do this: with open['somefile'] as f: for line in f: process[line]
4# warning -- not idiomatic Python! See below... with open[filename, 'rb'] as f: while True: line = f.readline[] if not line: break process[line]
74while [[ch != '\n'] && [ch != EOF]] { // read the next ch and add to a buffer // .. }
08# warning -- not idiomatic Python! See below... with open[filename, 'rb'] as f: while True: line = f.readline[] if not line: break process[line]
05# warning -- not idiomatic Python! See below... with open[filename, 'rb'] as f: while True: line = f.readline[] if not line: break process[line]
06# warning -- not idiomatic Python! See below... with open[filename, 'rb'] as f: while True: line = f.readline[] if not line: break process[line]
07# warning -- not idiomatic Python! See below... with open[filename, 'rb'] as f: while True: line = f.readline[] if not line: break process[line]
while line := f.readline[]:
process[line]
0# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
6 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
7# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
8 while [line = f.readline[]]:
# syntax error
04Phươ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 một danh sách. Hàm 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 nó thành các dòng riêng biệt. Chúng ta có thể lặp lại danh sách và dải ký tự mới \ n, bằng cách sử dụng hàm Dải [].
Example:
Python3
# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
3# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 # warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
5# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
6# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
8# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
6while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
1while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
2# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
4while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
6# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
8while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
9 while [line = f.readline[]]:
# syntax error
0 while [line = f.readline[]]:
# syntax error
1while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
2# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
4while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
6# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7 while [line = f.readline[]]:
# syntax error
8while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
9# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
0# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
2# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
3# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
5# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
6 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
7# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
8 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
9 while line := f.readline[]:
process[line]
0# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
3 while line := f.readline[]:
process[line]
22 while line := f.readline[]:
process[line]
4 while line := f.readline[]:
process[line]
0 while line := f.readline[]:
process[line]
6while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5 while line := f.readline[]:
process[line]
8 while line := f.readline[]:
process[line]
9Line1: Geeks Line2: for Line3: Geeks0
Line1: Geeks Line2: for Line3: Geeks1
Output:
Line1: Geeks Line2: for Line3: Geeks
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ả lại dưới dạng chuỗi. Nó có một tham số n, trong đó chỉ định số lượng 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ó tìm thấy từng dòng. readline [] trả về dòng tiếp theo của tệp có chứa ký tự dòng mới cuối cùng. Ngoài ra, nếu kết thúc của tệp, nó sẽ trả về một chuỗi trống.
Example:
Python3
# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
3# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 # warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
5# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
6# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
8# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
6while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
1while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
2# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
4while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
6# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
8while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
9Line1 Geeks Line2 for Line3 Geeks9
while [line = f.readline[]]:
# syntax error
1while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
2# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
4while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
6# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7 while [line = f.readline[]]:
# syntax error
8while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
9# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
3# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
5# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
6 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
7# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
8 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
9 while line := f.readline[]:
process[line]
0# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
3 while line := f.readline[]:
process[line]
22 while line := f.readline[]:
process[line]
4Phươ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ả lại dưới dạng chuỗi. Nó có một tham số n, trong đó chỉ định số lượng 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ó tìm thấy từng dòng. readline [] trả về dòng tiếp theo của tệp có chứa ký tự dòng mới cuối cùng. Ngoài ra, nếu kết thúc của tệp, nó sẽ trả về một chuỗi trống.
# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
08# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
09['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']2
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']3
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']4
while [line = f.readline[]]:
# syntax error
1Output:
Line1 Geeks Line2 for Line3 Geeks
while line := f.readline[]:
process[line]
0# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
7# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 # warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
03
while line := f.readline[]:
process[line]
# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
while line := f.readline[]:
process[line]
0# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
05 # warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
06 # warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
07Example:
Python3
# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
3# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 # warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
5# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
6# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
8# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
6while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
1while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
2# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
4while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
6# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
8while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
9 while [line = f.readline[]]:
# syntax error
0 while [line = f.readline[]]:
# syntax error
1while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
2# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
4while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
6# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7 while [line = f.readline[]]:
# syntax error
8while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
9# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
3# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
5# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
6 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
7# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
8 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
9 while line := f.readline[]:
process[line]
0# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
3 while line := f.readline[]:
process[line]
22 while line := f.readline[]:
process[line]
4 while line := f.readline[]:
process[line]
0# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
3 while line := f.readline[]:
process[line]
22 while line := f.readline[]:
process[line]
4 while line := f.readline[]:
process[line]
0 while line := f.readline[]:
process[line]
6while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5 while line := f.readline[]:
process[line]
8 while line := f.readline[]:
process[line]
9Line1: Geeks Line2: for Line3: Geeks0
Line1: Geeks Line2: for Line3: Geeks1
while [line = f.readline[]]:
# syntax error
1Output:
Using for loop Line1: Geeks Line2: for Line3: Geeks
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ả lại dưới dạng chuỗi. Nó có một tham số n, trong đó chỉ định số lượng 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ó tìm thấy từng dòng. readline [] trả về dòng tiếp theo của tệp có chứa ký tự dòng mới cuối cùng. Ngoài ra, nếu kết thúc của tệp, nó sẽ trả về một chuỗi trống.
['Geeks\n', 'For\n', 'Geeks']
['Geeks', 'For', 'Geeks']
2 ['Geeks\n', 'For\n', 'Geeks']
['Geeks', 'For', 'Geeks']
3['Geeks\n', 'For\n', 'Geeks']
['Geeks', 'For', 'Geeks']
4
Python3
# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
69while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
4while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
6# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
73 while line := f.readline[]:
process[line]
0# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
7# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 # warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
03 while line := f.readline[]:
process[line]
6# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
83# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
69while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
4while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
6# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
73 while line := f.readline[]:
process[line]
0# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
05 # warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
06 # warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
07Các
Output:
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']
Phương pháp 3: Đọc từng dòng tệp bằng cách sử dụng cho vòng lặp
Một đối tượng có thể được trả về bởi hàm Open [] trong khi mở một tệp. Cách cuối cùng để đọc từng dòng tệp này bao gồm lặp lại trên một đối tượng tệp trong một vòng lặp. Khi làm điều này, chúng tôi đang tận dụng chức năng Python tích hợp cho phép chúng tôi lặp lại đối tượng tệp hoàn toàn bằng cách sử dụng vòng lặp For trong 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ó ít dòng mã hơn, đây luôn là cách thực hành tốt nhất xứng đáng theo dõi.
Example:
Python3
# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
3# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 # warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
5# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
6# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
8# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
6while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
1# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
69while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
4while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5____211# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
7while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
13while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
14 while line := f.readline[]:
process[line]
0while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
16# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
3# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
5 while line := f.readline[]:
process[line]
6while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
22while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
9# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
69while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
4while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
11while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
14 while line := f.readline[]:
process[line]
0# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
0# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
32 while line := f.readline[]:
process[line]
0# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
6 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
7# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
8 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
9# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
08# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
3 while line := f.readline[]:
process[line]
22 while line := f.readline[]:
process[line]
4# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
08 while line := f.readline[]:
process[line]
6while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5 while line := f.readline[]:
process[line]
8 while line := f.readline[]:
process[line]
9Line1: Geeks Line2: for Line3: Geeks0
Line1: Geeks Line2: for Line3: Geeks1
# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
3# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
5 while line := f.readline[]:
process[line]
6while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
55while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
9# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
69while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
4while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
11while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
14 while line := f.readline[]:
process[line]
0# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
0# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
32# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
08# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
3 while line := f.readline[]:
process[line]
22 while line := f.readline[]:
process[line]
4 while line := f.readline[]:
process[line]
0____92 ['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']3
['Geeks\n', 'For\n', 'Geeks'] ['Geeks', 'For', 'Geeks']4
# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
08# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
7# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
74while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
79# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
09# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
08 while line := f.readline[]:
process[line]
6while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5 while line := f.readline[]:
process[line]
8 while line := f.readline[]:
process[line]
9Line1: Geeks Line2: for Line3: Geeks0
Line1: Geeks Line2: for Line3: Geeks1
# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
3# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 # THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
5 while line := f.readline[]:
process[line]
6while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
93while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
9# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
69while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
4while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
11while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
14 while line := f.readline[]:
process[line]
0# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
0# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
4 while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
32# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
08# THIS IS IDIOMATIC Python. Do this:
with open['somefile'] as f:
for line in f:
process[line]
3 while line := f.readline[]:
process[line]
22 while line := f.readline[]:
process[line]
4# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
08 while line := f.readline[]:
process[line]
6while [[ch != '\n'] && [ch != EOF]] {
// read the next ch and add to a buffer
// ..
}
5 while line := f.readline[]:
process[line]
8 while line := f.readline[]:
process[line]
9Line1: Geeks Line2: for Line3: Geeks0
Line1: Geeks Line2: for Line3: Geeks1
Output:
# warning -- not idiomatic Python! See below...
with open[filename, 'rb'] as f:
while True:
line = f.readline[]
if not line:
break
process[line]
0