Hướng dẫn how do you search for a line in a text file in python? - làm cách nào để bạn tìm kiếm một dòng trong tệp văn bản trong python?

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ớ.

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

    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    0
    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
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    3
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    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.

    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    5
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    6
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    7
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    8

    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    5
    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    0
    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    1
    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    2
    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    3

    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    5
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    6
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    7
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    8
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    9

    string = "GEEK FOR GEEKS"
    
    Input: "FOR" 
    Output: Yes, FOR is present in the given string.
    0
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    6
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    7
    string = "GEEK FOR GEEKS"
    
    Input: "FOR" 
    Output: Yes, FOR is present in the given string.
    3
    string = "GEEK FOR GEEKS"
    
    Input: "FOR" 
    Output: Yes, FOR is present in the given string.
    4

    Output:

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

    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    4
    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    5
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    7
    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    7

    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    4
    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    9
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    0
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    7
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    7
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    3
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    4

    Python3

    with open

    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    0
    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
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    3
    -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
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    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 [].

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

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

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

    Output:

    string does not exist

    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    4
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    55
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    7
    -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
    8
    Search for a String in Text Files using enumerate[]

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

    Python3

    with open

    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    0with 0
    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
    
    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.

    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    5
    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    0 with 6
    with open["file.txt", "r"] as fp:
        for line in lines_that_equal["my_string", fp]:
            print line
    
    2 with 8with 9

    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    5
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    01

    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    5
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    6
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    7
    def generate_lines_that_equal[string, fp]:
        for line in fp:
            if line == string:
                yield line
    
    05
    with open["file.txt", "r"] as fp:
        for line in generate_lines_that_equal["my_string", fp]:
            print line
    
    9

    Output:

    string does not exist in a file

    Làm thế nào để bạn tìm kiếm một dòng trong Python?

    Phương pháp 1: Tìm chỉ mục của chuỗi trong tệp văn bản bằng cách sử dụ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.using readline[] In this method, we are using the readline[] function, and checking with the find[] function, this method returns -1 if the value is not found and if found it returns 0.

    Làm thế nào để bạn kiểm tra xem một dòng có mặt trong một tệp trong Python không?

    Làm thế nào để kiểm tra xem một văn bản cụ thể có tồn tại trên tệp TXT trong câu trả lời mã Python hay không..
    File = Open ["search.txt"].
    In [Tệp. Đọc []].
    search_word = input ["Nhập một từ bạn muốn tìm kiếm trong tệp:"].
    if [search_word trong tệp. Đọc []]:.
    in ["từ tìm thấy"].
    in ["từ không tìm thấy"].

    Làm thế nào để bạn tìm thấy chỉ số của một dòng trong Python?

    Sử dụng Enumerate [] để có được số dòng của một cụm từ nhất định trong một tệp.Sử dụng Mở [Tệp, Chế độ] với Tệp làm Tên đường dẫn của Tệp và Chế độ là "R" để mở tệp để đọc.Gọi liệt kê [có thể lặp lại] với Iterable là tệp để có được một đối tượng liệt kê.. Use open[file, mode] with file as the pathname of the file and mode as "r" to open the file for reading. Call enumerate[iterable] with iterable as the file to get an enumerate object.

    Làm cách nào để tìm thấy một văn bản cụ thể trong Python?

    Phương pháp tìm [truy vấn] được tích hợp theo Python tiêu chuẩn.Chỉ cần gọi phương thức trên đối tượng Chuỗi để tìm kiếm một chuỗi, như vậy: obj.find [Tìm kiếm tìm kiếm].Phương thức Find [] tìm kiếm một chuỗi truy vấn và trả về vị trí ký tự nếu được tìm thấy.obj. find[“search”]. The find[] method searches for a query string and returns the character position if found.

    Bài Viết Liên Quan

    Chủ Đề