Hướng dẫn how read line at end of file python? - làm thế nào để đọc dòng ở cuối tệp python?

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:  

  •  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
       // ..
    }
    
    74
  • # 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]
    
    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

 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
04

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 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]
6
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
1

while [[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
   // ..
}
4
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[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 [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
8
while [[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
1

while [[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
   // ..
}
4
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[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
8
while [[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]
6
while [[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]
9
Line1: Geeks
Line2: for
Line3: Geeks
0
Line1: Geeks
Line2: for
Line3: Geeks
1

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]
6
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
1

while [[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
   // ..
}
4
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[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 [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
8
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
9

Line1 Geeks
Line2 for
Line3 Geeks
9

 while [line = f.readline[]]:
     # syntax error
1

while [[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
   // ..
}
4
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[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
8
while [[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

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.

# 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
1

Output:  

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]
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]
07

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]
6
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
1

while [[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
   // ..
}
4
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[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 [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
8
while [[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
1

while [[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
   // ..
}
4
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[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
8
while [[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]
6
while [[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]
9
Line1: Geeks
Line2: for
Line3: Geeks
0
Line1: Geeks
Line2: for
Line3: Geeks
1

 while [line = f.readline[]]:
     # syntax error
1

Output:

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]
69
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
4
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[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]
69
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
4
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[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]
07

Cá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]
6
while [[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]
69
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
4
while [[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]
7
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
13
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
14

 while line := f.readline[]:
     process[line]
0
while [[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]
6
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
22
while [[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]
69
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
4
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
11
while [[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]
6
while [[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]
9
Line1: Geeks
Line2: for
Line3: Geeks
0
Line1: Geeks
Line2: for
Line3: Geeks
1

# 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]
6
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
55
while [[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]
69
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
4
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
11
while [[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
   // ..
}
74

while [[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]
6
while [[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]
9
Line1: Geeks
Line2: for
Line3: Geeks
0
Line1: Geeks
Line2: for
Line3: Geeks
1

# 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]
6
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
93
while [[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]
69
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
4
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
5
while [[ch != '\n'] && [ch != EOF]] {
   // read the next ch and add to a buffer
   // ..
}
11
while [[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]
6
while [[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]
9
Line1: Geeks
Line2: for
Line3: Geeks
0
Line1: Geeks
Line2: for
Line3: Geeks
1

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

Readline có trả lại EOF trong Python không?

Kiểm tra kết thúc tệp bằng phương thức readline [], phương thức read [] không kích hoạt điều kiện cuối tệp. Thay vào đó, khi dữ liệu cạn kiệt, nó sẽ trả về một chuỗi trống. Lưu ý rằng chúng ta cũng phải loại bỏ ký tự dòng mới và chuyển đổi chuỗi thành số nguyên.The readline[] method doesn't trigger the end-of-file condition. Instead, when data is exhausted, it returns an empty string. Notice that we must also remove the new-line character and convert the string to an integer.

Làm cách nào để đọc một dòng từ một tệp trong Python?

Sử dụng readlines [] để đọc phạm vi dòng từ tệp, phương thức readlines [] đọc tất cả các dòng từ một tệp và lưu trữ nó trong danh sách.Bạn có thể sử dụng một số chỉ mục làm số dòng để trích xuất một tập hợp các dòng từ nó.Đây là cách đơn giản nhất để đọc một dòng cụ thể từ một tệp trong Python. The readlines[] method reads all lines from a file and stores it in a list. You can use an index number as a line number to extract a set of lines from it. This is the most straightforward way to read a specific line from a file in Python.

Làm cách nào để đọc dòng cuối cùng của một tệp trong Python?

Để đọc dòng cuối cùng của một tệp trong Python mà không lưu trữ toàn bộ tệp trong bộ nhớ: Vòng lặp qua từng dòng trong tệp.skip mỗi dòng cho đến khi bạn đạt được dòng cuối cùng. Dòng cuối cùng trong bộ nhớ.Loop through each line in the file. Skip each line until you reach the last line. Save the last line in memory.

Làm cách nào để đọc 5 dòng cuối cùng của một tệp trong Python?

Làm thế nào để có được các dòng N cuối cùng của một tệp trong Python..
a_file = open ["example.txt", "r"].
dòng = a_file.Đoạn đọc [].
last_lines = dòng [-5:].
print[last_lines].
một tập tin.gần[].

Bài Viết Liên Quan

Chủ Đề