Thao tác với các tập tin trong python

Chúng tôi cần biết các chi tiết cụ thể về cách tệp được định dạng để đưa ra câu trả lời chính xác, nhưng đây là một cách có thể hữu ích

Nội dung chính Hiển thị

  • Làm cách nào để trích xuất các phần cụ thể của tệp văn bản trong Python?
  • Làm cách nào để đọc một phần cụ thể của tệp bằng Python?
  • Làm cách nào để trích xuất một dòng cụ thể từ một tệp trong Python?
  • Làm cách nào để trích xuất văn bản từ một từ cụ thể trong Python?

Đầu tiên, 'thông tin' của bạn hiện chỉ là một đối tượng TextIOWrapper. Bạn có thể biết bằng cách chạy

ID = input["please enter a reference id to search for the patient: "]
info = open["data.txt", 'r'].read[]
if ID in info:
    #find[] returns the beginning index of a string
    f = info.find[ID]
    goods = info[f:]
    l = goods.find['Patient']
    goods = goods[:l]
    print[goods]   
else:
    print["not in file"]
7. Bạn cần làm cho nó có tên là
ID = input["please enter a reference id to search for the patient: "]
info = open["data.txt", 'r'].read[]
if ID in info:
    #find[] returns the beginning index of a string
    f = info.find[ID]
    goods = info[f:]
    l = goods.find['Patient']
    goods = goods[:l]
    print[goods]   
else:
    print["not in file"]
8 để cung cấp cho bạn một chuỗi văn bản hoặc
ID = input["please enter a reference id to search for the patient: "]
info = open["data.txt", 'r'].read[]
if ID in info:
    #find[] returns the beginning index of a string
    f = info.find[ID]
    goods = info[f:]
    l = goods.find['Patient']
    goods = goods[:l]
    print[goods]   
else:
    print["not in file"]
9 để cung cấp cho bạn danh sách văn bản theo dòng, nếu định dạng chỉ là văn bản thuần túy

Giả sử dữ liệu trông giống như thế này

Patient: Charlie
Age = 99
Description: blah blah blah
Patient: Judith
Age: 100
Description: blah blah blahs

Bạn có thể làm như sau

Đầu tiên, tìm và lưu trữ chỉ mục của ID bạn đang tìm kiếm. Thứ hai, tìm và lưu trữ chỉ mục của một số chuỗi biểu thị ID mới. Trong trường hợp này, đó là từ 'Bệnh nhân'. Cuối cùng, trả về chuỗi giữa hai chỉ số đó

Thí dụ

ID = input["please enter a reference id to search for the patient: "]
info = open["data.txt", 'r'].read[]
if ID in info:
    #find[] returns the beginning index of a string
    f = info.find[ID]
    goods = info[f:]
    l = goods.find['Patient']
    goods = goods[:l]
    print[goods]   
else:
    print["not in file"]

Một cái gì đó dọc theo những dòng đó sẽ thực hiện thủ thuật. Có thể có nhiều cách tốt hơn tùy thuộc vào cấu trúc của tệp. Mọi thứ có thể sai nếu đầu vào của người dùng không đủ cụ thể hoặc từ bệnh nhân nằm rải rác trong các mô tả, nhưng ý tưởng vẫn giữ nguyên. Bạn cũng nên thực hiện một số xử lý lỗi cho đầu vào. Tôi hy vọng điều đó sẽ giúp. Chúc may mắn với dự án của bạn

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ác tệp văn bản bao gồm nội dung văn bản thuần túy. 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, số dòng bắt đầu bằng chỉ số thứ 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.  

    Tập tin đang sử dụng. kiểm tra. txt

    Phương pháp 1. đối tượng tập tin. đường đọc []

    Một đối tượng tệp có thể được tạo bằng Python và sau đó phương thức readlines[] có thể được gọi trên đối tượng này để đọc các dòng thành một luồng. Phương pháp này được ưu tiên khi một dòng hoặc một loạt các dòng từ mộ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 đến một số chỉ mục kết thúc. Ban đầu, nó đọ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.  

    Thí dụ

    Python3

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    0
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    1
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    0____11
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    2
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    3

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    4
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    1
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    0
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    7

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    8
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    1____100
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    3

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    8
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    03____104
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    05

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    8
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    1____108
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    3

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    8
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    03____222
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    23
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    24
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    05

    đầu ra

    dòng thứ mười
     

    Đây là dòng 10

    ba dòng đầu tiên
     

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

    Phương pháp 2. gói linecache

    Gói linecache có thể được nhập bằng 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 nhiều dòng cùng một lúc. Nó sử dụng bộ nhớ cache để thực hiện tối ưu hóa nội bộ. Gói này tự mở tệp và chuyển đến dòng cụ thể. Gói này có phương thức getline[] được sử dụng cho cùng.  

    cú pháp.  

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    0

    Thí dụ

    Python3

    ________ 226 ________ 227

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    28
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    1
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    00
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    2
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    02
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    03
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    3

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    8
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    06

    đầu ra

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    2

    Phương pháp 3. liệt kê[]

    Phương thức enumerate[] được sử dụng để chuyển đổi một chuỗi hoặc một đối tượng danh sách thành một chuỗi dữ liệu được lập chỉ mục bởi các số. Sau đó, nó được sử dụng trong việc liệt kê dữ liệu kết hợp với vòng lặp for. 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.  

    Thí dụ

    Python3

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    0
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    1
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    0
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    1
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    71
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    3

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    73
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    1
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    75_______222____302
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    78____302
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    80
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    81

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    82
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    83
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    84
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    85
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    1
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    0
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    88

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    89
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    90
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    91
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    84
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    93

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    94
    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    8____496

    đầu ra

    ID = input["please enter a reference id to search for the patient: "]
    info = open["data.txt", 'r'].read[]
    if ID in info:
        #find[] returns the beginning index of a string
        f = info.find[ID]
        goods = info[f:]
        l = goods.find['Patient']
        goods = goods[:l]
        print[goods]   
    else:
        print["not in file"]
    
    0

    Làm cách nào để trích xuất các phần cụ thể của tệp văn bản 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 .

    Đảm bảo bạn đang sử dụng Python 3

    Đọc dữ liệu 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 chính quy

    Để tất cả chúng cùng nhau

    Làm cách nào để đọc một phần cụ thể của tệp bằng Python?

    Phương pháp 1. đối tượng tập tin. readlines[] Một đối tượng tệp có thể được tạo trong Python và sau đó phương thức readlines[] có thể được gọi trên đối tượng này để đọc các dòng trong một luồng. Phương pháp này được ưu tiên khi một dòng hoặc một loạt các dòng từ một tệp cần được truy cập đồng thời.

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

    Sử dụng readlines[] để đọc phạm vi dòng từ Tệp Phương thức readlines[] đọc tất cả các dòng từ một tệp và lưu trữ nó trong một danh sách. Bạn có thể sử dụng số chỉ mục làm số dòng để trích xuất một tập hợp các dòng từ nó. Đây là cách đơn giản nhất để đọc một dòng cụ thể từ một tệp trong Python.

    Làm cách nào để trích xuất văn bản từ một từ cụ thể trong Python?

    Sử dụng biểu thức chính quy để trích xuất bất kỳ từ cụ thể nào Chúng tôi có thể sử dụng phương thức search[] từ mô-đun re để tìm lần xuất hiện đầu tiên của từ đó và sau đó chúng tôi có thể lấy từ đó bằng cách cắt

    Chủ Đề