Hướng dẫn how to find a specific word in a text file in python - cách tìm một từ cụ thể trong tệp văn bản bằng python

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

    MemTotal,5,2016-07-30 12:02:33,781
    model name,3,2016-07-30 13:37:59,074
    model,3,2016-07-30 15:39:59,075
    
    0
    string does not exist in a file
    6
    string does not exist in a file
    7
    MemTotal,5,2016-07-30 12:02:33,781
    model name,3,2016-07-30 13:37:59,074
    model,3,2016-07-30 15:39:59,075
    
    3
    MemTotal,5,2016-07-30 12:02:33,781
    model name,3,2016-07-30 13:37:59,074
    model,3,2016-07-30 15:39:59,075
    
    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
    term = "model"
    file = open['file.txt']
    for line in file:
        line.strip[].split['/n']
        if term in line:
            print line
    file.close[]
    
    1
    term = "model"
    file = open['file.txt']
    for line in file:
        line.strip[].split['/n']
        if term in line:
            print line
    file.close[]
    
    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
    model name,3,2016-07-30 13:37:59,074
    model,3,2016-07-30 15:39:59,075
    
    7
    string does not exist in a file
    9

    string does not exist
    4
     model,3,2016-07-30 15:39:59,075
    
    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
     model,3,2016-07-30 15:39:59,075
    
    5
    string does not exist in a file
    9

    Output:

    string does not exist

    string does not exist
    4
    term = "model"
    file = open['file.txt']
    for line in file:
        line.strip[].split['/n']
        if term in line:
            print line
    file.close[]
    
    55
    -1
    -1
    0
    string exists in file
    line Number: 4
    -1
    -1
    7
    term = "model"
    file = open['file.txt']
    for line in file:
        line.strip[].split['/n']
        if term in line:
            print line
    file.close[]
    
    2
    term = "model"
    file = open['file.txt']
    for line in file:
        line.strip[].split['/n']
        if term in line:
            print line
    file.close[]
    
    8
    Search for a String in Text Files using enumerate[]

    string does not exist
    4
    string does not exist
    9
    model name,3,2016-07-30 13:37:59,074
    model,3,2016-07-30 15:39:59,075
    
    1
    string does not exist
    2
    model name,3,2016-07-30 13:37:59,074
    model,3,2016-07-30 15:39:59,075
    
    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

    Tôi có tệp văn bản này:

    MemTotal,5,2016-07-30 12:02:33,781
    model name,3,2016-07-30 13:37:59,074
    model,3,2016-07-30 15:39:59,075
    

    Tôi cần tìm dòng với mô hình.

    Mã của tôi:

    term = "model"
    file = open['file.txt']
    for line in file:
        line.strip[].split['/n']
        if term in line:
            print line
    file.close[]
    

    Đây là đầu ra:

    model name,3,2016-07-30 13:37:59,074
    model,3,2016-07-30 15:39:59,075
    

    Tôi chỉ cần dòng này làm đầu ra:

     model,3,2016-07-30 15:39:59,075
    

    Tôi có thể làm cái này như thế nào?

    Làm cách nào để tìm một từ cụ thể trong một tệp văn bản?

    Để mở khung tìm từ chế độ xem chỉnh sửa, nhấn Ctrl+F hoặc nhấp vào nhà> Tìm. Tìm văn bản bằng cách nhập nó vào tìm kiếm tài liệu cho hộp. Ứng dụng Web Web bắt đầu tìm kiếm ngay khi bạn bắt đầu gõ.press Ctrl+F, or click Home > Find. Find text by typing it in the Search the document for… box. Word Web App starts searching as soon as you start typing.

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

    Cách trích xuất các phần cụ thể của tệp văn bản bằng Python..
    Hãy chắc chắn rằng bạn đang sử dụng Python 3 ..
    Đọc dữ liệu từ một tệp văn bản ..
    Sử dụng "với mở".
    Đọc các tệp văn bản theo từng dòng ..
    Lưu trữ dữ liệu văn bản trong một biến ..
    Tìm kiếm văn bản cho một chuỗi con ..
    Kết hợp các biểu thức thường xuyên ..
    Để tất cả chúng cùng nhau..

    Làm thế nào để bạn tìm kiếm một tệp cho một chuỗi văn bản cụ thể?

    Bạn cần sử dụng lệnh grep.Lệnh GREP hoặc lệnh EGREP tìm kiếm các tệp đầu vào đã cho cho các dòng chứa khớp hoặc chuỗi văn bản.use the grep command. The grep command or egrep command searches the given input FILEs for lines containing a match or a text string.

    Làm thế nào để bạn tìm kiếm văn bản trong Python?

    Phương thức Find [] chuỗi Python trả về chỉ số thấp nhất hoặc lần xuất hiện đầu tiên của chuỗi con nếu nó được tìm thấy trong một chuỗi nhất định.Nếu nó không được tìm thấy, thì nó sẽ trả về -1.Tham số: Sub: Subring cần được tìm kiếm trong chuỗi đã cho.. If it is not found, then it returns -1. Parameters: sub: Substring that needs to be searched in the given string.

    Bài Viết Liên Quan

    Chủ Đề