Hướng dẫn how to print a certain number of lines in python - cách in một số dòng nhất định trong python

Tôi không biết làm thế nào để in một số dòng mã từ tệp văn bản. Tất cả không làm gì khi tôi chạy mã dưới đây. Làm thế nào để đọc các dòng cụ thể từ tệp?.

Show
    line = open ("random.txt",2)
    print(line)
    line = open ("random.txt",5)
    print(line)
    line = open ("random.txt",7)
    print(line)
    line = open ("random.txt",9)
    print(line)
    line = open ("random.txt",10)
    print(line)
    myFile.close()
    

    Đầu ra của tôi có thể là chuỗi ngẫu nhiên và tôi không bận tâm miễn là nó chỉ in tập hợp các dòng.

    Hướng dẫn how to print a certain number of lines in python - cách in một số dòng nhất định trong python

    hỏi ngày 21 tháng 2 năm 2016 lúc 20:35Feb 21, 2016 at 20:35

    Hướng dẫn how to print a certain number of lines in python - cách in một số dòng nhất định trong python

    0

    open lấy chế độ làm đối số thứ hai của nó, không phải là một số. Tôi nghĩ những gì bạn cần là đây:

    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    

    Khối with sẽ chăm sóc đóng tệp.

    Đã trả lời ngày 21 tháng 2 năm 2016 lúc 20:39Feb 21, 2016 at 20:39

    Hướng dẫn how to print a certain number of lines in python - cách in một số dòng nhất định trong python

    Zondozondozondo

    Huy hiệu vàng 19.4K77 gold badges43 silver badges83 bronze badges

    2

    wanted_lines = [2,5,7,9,10]
    count = 1
    with open('random.txt', 'r') as infile:
         for line in infile:
              line = line.strip()
              if count in wanted_lines:
                   print(line)
              else:
                   pass
              count += 1
    

    Đã trả lời ngày 21 tháng 2 năm 2016 lúc 20:41Feb 21, 2016 at 20:41

    SeekheartseekheartSeekheart

    1.0977 Huy hiệu bạc8 Huy hiệu đồng7 silver badges8 bronze badges

    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 print a certain number of lines in python - cách in một số dòng nhất định 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

    file = open

    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    1
    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    2
    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    3

    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    4= file
    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    7

    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    8
    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    1
    wanted_lines = [2,5,7,9,10]
    count = 1
    with open('random.txt', 'r') as infile:
         for line in infile:
              line = line.strip()
              if count in wanted_lines:
                   print(line)
              else:
                   pass
              count += 1
    
    0
    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    3

    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    8
    wanted_lines = [2,5,7,9,10]
    count = 1
    with open('random.txt', 'r') as infile:
         for line in infile:
              line = line.strip()
              if count in wanted_lines:
                   print(line)
              else:
                   pass
              count += 1
    
    3
    wanted_lines = [2,5,7,9,10]
    count = 1
    with open('random.txt', 'r') as infile:
         for line in infile:
              line = line.strip()
              if count in wanted_lines:
                   print(line)
              else:
                   pass
              count += 1
    
    4
    wanted_lines = [2,5,7,9,10]
    count = 1
    with open('random.txt', 'r') as infile:
         for line in infile:
              line = line.strip()
              if count in wanted_lines:
                   print(line)
              else:
                   pass
              count += 1
    
    5

    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    8
    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    1
    wanted_lines = [2,5,7,9,10]
    count = 1
    with open('random.txt', 'r') as infile:
         for line in infile:
              line = line.strip()
              if count in wanted_lines:
                   print(line)
              else:
                   pass
              count += 1
    
    8
    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    3

    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    8
    wanted_lines = [2,5,7,9,10]
    count = 1
    with open('random.txt', 'r') as infile:
         for line in infile:
              line = line.strip()
              if count in wanted_lines:
                   print(line)
              else:
                   pass
              count += 1
    
    3
    getLine(txt-file, line_number)
    2
    getLine(txt-file, line_number)
    3
    getLine(txt-file, line_number)
    4
    wanted_lines = [2,5,7,9,10]
    count = 1
    with open('random.txt', 'r') as infile:
         for line in infile:
              line = line.strip()
              if count in wanted_lines:
                   print(line)
              else:
                   pass
              count += 1
    
    5

    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

    getLine(txt-file, line_number)
    6
    getLine(txt-file, line_number)
    7

    getLine(txt-file, line_number)
    8=
    This is line 5.
    0
    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    2
    This is line 5.
    2
    This is line 5.
    3__

    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    8
    This is line 5.
    6

    Đầ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

    file = open

    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    1
    This is line 1.
    This is line 8.
    This is line 12.
    1
    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    3

    This is line 1.
    This is line 8.
    This is line 12.
    3=
    This is line 1.
    This is line 8.
    This is line 12.
    5
    getLine(txt-file, line_number)
    2
    This is line 5.
    2
    This is line 1.
    This is line 8.
    This is line 12.
    8
    This is line 5.
    2open0open1

    open2 open3open4 open5

    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    1fileopen8

    open9with0 with1open4 with3

    with4

    with open("random.txt") as open_file:
        lines = open_file.readlines()
        print(lines[2])
        print(lines[5])
        ...
    
    8with6

    Đầu ra

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