Hướng dẫn how to read a range of lines from a file in python? - cách đọc một loạt các dòng từ một tệp trong python?

Có thể đọc từ một tệp văn bản, một phạm vi dòng đặt ví dụ từ dòng 20 đến 52 không?

Show

    Tôi đang mở tệp và đọc tệp như thế này:

    text_file = open(file_to_save, "r")
    line = text_file.readline()
    

    nhưng chỉ muốn lưu dữ liệu trong một phạm vi đã đặt hoặc nếu có thể đọc từ một dòng sau một dòng chứa --- dữ liệu --- sang một dòng khác có chứa-kết thúc dữ liệu-

    Tôi đã xem qua các tài liệu và các ví dụ trực tuyến và chỉ có thể tìm thấy các ví dụ chỉ định một dòng mới hoặc một cái gì đó

    Hướng dẫn how to read a range of lines from a file in python? - cách đọc một loạt các dòng từ một tệp trong python?

    Đã hỏi ngày 12 tháng 7 năm 2014 lúc 18:24Jul 12, 2014 at 18:24

    2

    Bạn có thể sử dụng itertools.islice() trên đối tượng tệp và sử dụng lặp lại để chỉ đọc các dòng cụ thể:

    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    

    sẽ đọc các dòng từ 20 đến 52; Python sử dụng lập chỉ mục dựa trên 0, vì vậy dòng 1 được đánh số 0.

    Sử dụng đối tượng tệp làm trình lặp cung cấp cho bạn rất nhiều tính linh hoạt; Bạn có thể đọc các dòng bổ sung bằng cách gọi next() trên đối tượng tệp, ví dụ, nâng cao dòng 'con trỏ' khi bạn làm như vậy.

    Khi sử dụng tệp như một điều khác, không sử dụng

    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    0; Hai kỹ thuật không dễ dàng trộn lẫn.

    Đã trả lời ngày 12 tháng 7 năm 2014 lúc 18:30Jul 12, 2014 at 18:30

    Martijn Pieters ♦ Martijn PietersMartijn Pieters

    998K280 Huy hiệu vàng3925 Huy hiệu bạc3264 Huy hiệu đồng280 gold badges3925 silver badges3264 bronze badges

    2

    Bạn có thể làm hai điều. Bạn có thể sử dụng

    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    1 và sử dụng câu lệnh
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    2:

    text_file = open(file_to_save, "r")
    lines = []
    for index, text in enumerate(text_file):
        if 19 <= index <= 51:
            lines.append(text)
    

    Hoặc thay vào đó, bạn có thể sử dụng

    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    3 và sau đó cắt lát:

    text_file = open(file_to_save, "r")
    lines = text_file.readlines()
    lines = lines[19:52]
    

    Alfonso

    86417 Huy hiệu bạc 30 Huy hiệu Đồng17 silver badges30 bronze badges

    Đã trả lời ngày 12 tháng 7 năm 2014 lúc 18:29Jul 12, 2014 at 18:29

    Hướng dẫn how to read a range of lines from a file in python? - cách đọc một loạt các dòng từ một tệp trong python?

    ZenofpythonzenofpythonZenOfPython

    8916 Huy hiệu bạc15 Huy hiệu Đồng6 silver badges15 bronze badges

    1

    HM, bạn có thể thử một cái gì đó trong khi vòng lặp .... nhưng bạn nên biết chuỗi nào bạn muốn văn bản được tải, đó không phải là cách tốt nhất:

    text_file=open(name,'r')
    line=text.readline()
    while line!=(" ") #type the line where you want it to stop
    print (line
    

    Đã trả lời ngày 12 tháng 7 năm 2014 lúc 18:47Jul 12, 2014 at 18:47

    Cải thiện bài viết

    Lưu bài viết

    Tệp văn bản bao gồm nội dung văn bản đơn giản. Tệp văn bản còn được gọi là tệp phẳng hoặc tệp đơn giản. Python cung cấp hỗ trợ dễ dàng để đọc và truy cập nội dung trong tệp. Các tệp văn bản được mở đầu tiên và sau đó nội dung được truy cập từ nó theo thứ tự các dòng. Theo mặc định, các số dòng bắt đầu với chỉ mục 0. Có nhiều cách khác nhau để đọc các dòng cụ thể từ một tệp văn bản trong Python, bài viết này nhằm mục đích thảo luận về chúng. & NBSP;

    Tệp đang sử dụng: test.txt test.txt

    Hướng dẫn how to read a range of lines from a file in python? - cách đọc một loạt các dòng từ một tệp trong python?

    Phương pháp 1: FileObject.ReadLines ()

    Một đối tượng tệp có thể được tạo trong python và sau đó readlines () phương thức có thể được gọi trên đối tượng này để đọc các dòng vào một luồng. Phương pháp này được ưa thích khi một dòng hoặc một phạm vi dòng từ tệp cần được truy cập đồng thời. Nó có thể dễ dàng được sử dụng để in các dòng từ bất kỳ chỉ mục bắt đầu ngẫu nhiên nào sang một số chỉ mục kết thúc. Nó ban đầu đọc toàn bộ nội dung của tệp và giữ một bản sao của nó trong bộ nhớ. Các dòng tại các chỉ số được chỉ định sau đó được truy cập. & NBSP;

    Example:

    Python3

    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    4
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    5
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    6
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    7
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    8
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    9

    text_file = open(file_to_save, "r")
    lines = []
    for index, text in enumerate(text_file):
        if 19 <= index <= 51:
            lines.append(text)
    
    0
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    5
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    4
    text_file = open(file_to_save, "r")
    lines = []
    for index, text in enumerate(text_file):
        if 19 <= index <= 51:
            lines.append(text)
    
    3

    text_file = open(file_to_save, "r")
    lines = []
    for index, text in enumerate(text_file):
        if 19 <= index <= 51:
            lines.append(text)
    
    4
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    7
    text_file = open(file_to_save, "r")
    lines = []
    for index, text in enumerate(text_file):
        if 19 <= index <= 51:
            lines.append(text)
    
    6
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    9

    text_file = open(file_to_save, "r")
    lines = []
    for index, text in enumerate(text_file):
        if 19 <= index <= 51:
            lines.append(text)
    
    4
    text_file = open(file_to_save, "r")
    lines = []
    for index, text in enumerate(text_file):
        if 19 <= index <= 51:
            lines.append(text)
    
    9
    text_file = open(file_to_save, "r")
    lines = text_file.readlines()
    lines = lines[19:52]
    
    0
    text_file = open(file_to_save, "r")
    lines = text_file.readlines()
    lines = lines[19:52]
    
    1

    text_file = open(file_to_save, "r")
    lines = []
    for index, text in enumerate(text_file):
        if 19 <= index <= 51:
            lines.append(text)
    
    4
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    7
    text_file = open(file_to_save, "r")
    lines = text_file.readlines()
    lines = lines[19:52]
    
    4
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    9

    text_file = open(file_to_save, "r")
    lines = []
    for index, text in enumerate(text_file):
        if 19 <= index <= 51:
            lines.append(text)
    
    4
    text_file = open(file_to_save, "r")
    lines = []
    for index, text in enumerate(text_file):
        if 19 <= index <= 51:
            lines.append(text)
    
    9
    text_file = open(file_to_save, "r")
    lines = text_file.readlines()
    lines = lines[19:52]
    
    8
    text_file = open(file_to_save, "r")
    lines = text_file.readlines()
    lines = lines[19:52]
    
    9
    text_file=open(name,'r')
    line=text.readline()
    while line!=(" ") #type the line where you want it to stop
    print (line
    
    0
    text_file = open(file_to_save, "r")
    lines = text_file.readlines()
    lines = lines[19:52]
    
    1

    Output 

    dòng thứ mười & nbsp;
     

    Đây là dòng 10.

    ba dòng đầu tiên & nbsp;
     

    Đây là dòng 1. Đây là dòng 2. Đây là dòng 3.

    Phương pháp 2: Gói Linecache & NBSP;

    Gói linecache có thể được nhập vào Python và sau đó được sử dụng để trích xuất và truy cập các dòng cụ thể trong Python. Gói có thể được sử dụng để đọc đồng thời nhiều dòng. Nó sử dụng lưu trữ bộ đệm để thực hiện tối ưu hóa nội bộ. Gói này tự mở tệp và đến dòng cụ thể. Gói này có phương thức GetLine () được sử dụng cho cùng. & NBSP;

    Syntax: 

    getLine(txt-file, line_number)

    Example:

    Python3

    text_file=open(name,'r')
    line=text.readline()
    while line!=(" ") #type the line where you want it to stop
    print (line
    
    2
    text_file=open(name,'r')
    line=text.readline()
    while line!=(" ") #type the line where you want it to stop
    print (line
    
    3

    text_file=open(name,'r')
    line=text.readline()
    while line!=(" ") #type the line where you want it to stop
    print (line
    
    4
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    5
    text_file=open(name,'r')
    line=text.readline()
    while line!=(" ") #type the line where you want it to stop
    print (line
    
    6
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    8
    text_file=open(name,'r')
    line=text.readline()
    while line!=(" ") #type the line where you want it to stop
    print (line
    
    8
    text_file=open(name,'r')
    line=text.readline()
    while line!=(" ") #type the line where you want it to stop
    print (line
    
    9
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    9

    text_file = open(file_to_save, "r")
    lines = []
    for index, text in enumerate(text_file):
        if 19 <= index <= 51:
            lines.append(text)
    
    4
    getLine(txt-file, line_number)
    2

    Đầu ra:

    This is line 5.

    Phương pháp 3: Enumate ()

    Phương thức liệt kê () được sử dụng để chuyển đổi một chuỗi hoặc đối tượng danh sách thành chuỗi dữ liệu được lập chỉ mục theo số. Sau đó, nó được sử dụng trong danh sách dữ liệu kết hợp với FOR LOOP. Các dòng tại các chỉ mục cụ thể có thể được truy cập bằng cách chỉ định các số chỉ mục cần thiết trong một mảng. & Nbsp;

    Example:

    Python3

    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    4
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    5
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    6
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    7
    getLine(txt-file, line_number)
    7
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    9

    getLine(txt-file, line_number)
    9
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    5
    This is line 5.
    1
    text_file = open(file_to_save, "r")
    lines = text_file.readlines()
    lines = lines[19:52]
    
    8
    text_file=open(name,'r')
    line=text.readline()
    while line!=(" ") #type the line where you want it to stop
    print (line
    
    8
    This is line 5.
    44444866666

    This is line 5.
    8
    This is line 5.
    9
    This is line 1.
    This is line 8.
    This is line 12.
    0
    This is line 1.
    This is line 8.
    This is line 12.
    1
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    7
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    4
    This is line 1.
    This is line 8.
    This is line 12.
    4

    This is line 1.
    This is line 8.
    This is line 12.
    5
    import itertools
    
    with open(file_to_save, "r") as text_file:
        for line in itertools.islice(text_file, 19, 52):
             # do something with line
    
    2
    This is line 1.
    This is line 8.
    This is line 12.
    77____70
    This is line 1.
    This is line 8.
    This is line 12.
    9

    itertools.islice()0

    text_file = open(file_to_save, "r")
    lines = []
    for index, text in enumerate(text_file):
        if 19 <= index <= 51:
            lines.append(text)
    
    4itertools.islice()2

    Đầu ra

    This is line 1.
    This is line 8.
    This is line 12.