Hướng dẫn python find string in text file and print line - python tìm chuỗi trong tệp văn bản và dòng in

Xem thảo luận

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

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

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

    Lưu bài viết

    Đọc

    Example:

    string = "GEEK FOR GEEKS"
    
    Input: "FOR" 
    Output: Yes, FOR is present in the given string.

    Bàn luận

    myfile.txt

    Trong bài viết này, chúng ta sẽ xem cách tìm kiếm một chuỗi trong các tệp văn bản bằng Python

    Tệp văn bản để trình diễn:

    Python3

    with open

    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    0
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    1
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    2
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    3
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    4

    Phương pháp 1: & nbsp; Tìm chỉ mục của chuỗi trong tệp văn bản bằng readline []

    Trong phương thức này, chúng tôi đang sử dụng hàm readline [] và kiểm tra hàm find [], phương thức này trả về -1 nếu không tìm thấy giá trị và nếu thấy nó trả về 0.

    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    5
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    6
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    7
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    8

    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    5
    string does not exist
    0
    string does not exist
    1
    string does not exist
    2
    string does not exist
    3

    string does not exist in a file
    5
    string does not exist in a file
    6
    string does not exist in a file
    7
    string does not exist in a file
    8
    string does not exist in a file
    9

    def lines_that_equal[line_to_match, fp]:
        return [line for line in fp if line == line_to_match]
    
    def lines_that_contain[string, fp]:
        return [line for line in fp if string in line]
    
    def lines_that_start_with[string, fp]:
        return [line for line in fp if line.startswith[string]]
    
    def lines_that_end_with[string, fp]:
        return [line for line in fp if line.endswith[string]]
    
    0
    string does not exist in a file
    6
    string does not exist in a file
    7
    def lines_that_equal[line_to_match, fp]:
        return [line for line in fp if line == line_to_match]
    
    def lines_that_contain[string, fp]:
        return [line for line in fp if string in line]
    
    def lines_that_start_with[string, fp]:
        return [line for line in fp if line.startswith[string]]
    
    def lines_that_end_with[string, fp]:
        return [line for line in fp if line.endswith[string]]
    
    3
    def lines_that_equal[line_to_match, fp]:
        return [line for line in fp if line == line_to_match]
    
    def lines_that_contain[string, fp]:
        return [line for line in fp if string in line]
    
    def lines_that_start_with[string, fp]:
        return [line for line in fp if line.startswith[string]]
    
    def lines_that_end_with[string, fp]:
        return [line for line in fp if line.endswith[string]]
    
    4

    Output:

    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1

    string does not exist
    4
    string does not exist
    5
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    7
    string does not exist
    7

    string does not exist
    4
    string does not exist
    9
    string does not exist in a file
    0
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    7
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    7
    string does not exist in a file
    3
    string does not exist in a file
    4

    Python3

    with open

    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    0
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    1
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    2
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    3
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    1
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    2
    string does not exist in a file
    4

    Phương pháp 2: Tìm chuỗi trong tệp văn bản bằng Read []

    Chúng tôi sẽ tìm kiếm chuỗi từng dòng nếu chuỗi được tìm thấy thì chúng tôi sẽ in số chuỗi và số đó bằng hàm read [].

    string does not exist in a file
    5
    string does not exist in a file
    6
    string does not exist in a file
    7
    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    7
    string does not exist in a file
    9

    string does not exist
    4
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    0
    string does not exist in a file
    4

    string does not exist in a file
    5
    string does not exist in a file
    6
    string does not exist in a file
    7
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    5
    string does not exist in a file
    9

    Output:

    string does not exist

    string does not exist
    4
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    55
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    7
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    2
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    8
    Search for a String in Text Files using enumerate[]

    string does not exist
    4
    string does not exist
    9
    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    1
    string does not exist
    2
    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    3

    Python3

    with open

    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    0with 0
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    2
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    3with 3

    Phương pháp 3: Tìm kiếm một chuỗi trong các tệp văn bản bằng cách sử dụng Enumerate []

    Chúng tôi chỉ tìm thấy chuỗi có mặt trong tệp hoặc không sử dụng Enumerate [] trong Python.

    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    5
    string does not exist
    0 with 6
    string does not exist
    2 with 8with 9

    string does not exist in a file
    5
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    01

    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    5
    string does not exist in a file
    6
    string does not exist in a file
    7
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    05
    string does not exist in a file
    9

    Output:

    string does not exist in a file

    Danh sách xây dựng các dòng phù hợp - một số hương vị:

    def lines_that_equal[line_to_match, fp]:
        return [line for line in fp if line == line_to_match]
    
    def lines_that_contain[string, fp]:
        return [line for line in fp if string in line]
    
    def lines_that_start_with[string, fp]:
        return [line for line in fp if line.startswith[string]]
    
    def lines_that_end_with[string, fp]:
        return [line for line in fp if line.endswith[string]]
    

    Build Trình tạo các dòng phù hợp [bộ nhớ hiệu quả]:

    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    

    In tất cả các dòng đối sánh [tìm tất cả các trận đấu trước, sau đó in chúng]:

    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    

    In tất cả các dòng phù hợp [in chúng một cách lười biếng, như chúng ta tìm thấy chúng]

    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    

    Máy phát điện [được sản xuất bởi năng suất] là bạn bè của bạn, đặc biệt là với các tệp lớn không phù hợp với bộ nhớ.

    Làm thế nào để bạn tìm thấy dòng của một chuỗi trong một tệp văn bản trong Python?

    Mở một tệp trong chế độ đọc. Tiếp theo, sử dụng phương thức readlines [] để lấy tất cả các dòng từ một tệp dưới dạng đối tượng danh sách. Tiếp theo, sử dụng một vòng lặp để lặp từng dòng từ một tệp. Tiếp theo, trong mỗi lần lặp của một vòng lặp, sử dụng điều kiện IF để kiểm tra xem một chuỗi có mặt trong một dòng hiện tại và in số dòng và dòng dòng hiện tại.

    Làm cách nào để in một dòng từ một tệp văn bản trong Python?

    Sử dụng tệp ...
    a_file = open ["sample.txt"].
    dòng = a_file. Đoạn đọc [].
    Đối với dòng trong dòng:.
    print[line].
    một tập tin. gần[].

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

    Phương pháp 1: Đọc một dòng theo từng dòng 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.using readlines[] readlines[] is used to read all the lines at a single go and then return them as each line a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines.

    Làm cách nào để trích xuất một dòng cụ thể từ một tệp trong Python?

    Phương thức 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.

    Bài Viết Liên Quan

    Chủ Đề