如何从多个文本文件中提取特定参数及其数据?

How to i extract a particular parameter and it's data from multiple text files?

假设我有多个文件,例如 file1 file2 和 file3,并且包含以下数据:

文件 1:

{"status":"succes","message":"User Found","error_code":"0","data":{"phone":0,"name":"Sanju Mehra","gender":"","tob":"","dob":"","pob":"","email":"sanjumehra.palasi@gmail.com"}

文件 2:

{"status":"succes","message":"User Found","error_code":"0","data":{"phone":0,"name":"Anil Kumar","gender":"","tob":"","dob":"","pob":"","email":"kumaranil12462@gmail.com"}

文件 3:

{"status":"succes","message":"User Found","error_code":"0","data":{"phone":89XXXXXXXX,"name":"Ashish Chauhan","gender":"male","tob":"12:30","dob":"10\/18\/94","pob":"ambala, haryana","email":"ashishchauhan1810@gmail.com"}

我想从这些 multipe 文件中提取名称和 phone 编号,我实际上想在其中提取 phone 编号的数据和在名称参数上输入的数据。

你能告诉我我可以用哪些方法来做到这一点吗?

文件中的数据看起来像 dictionary format。那么答案如下

import os,sys        

ws=r"C:\Users\xx\Desktop\dd"
for root,dirs,files in os.walk(ws):
    for file1 in files:
        file_path=os.path.join(root,file1)

        file2 = open(file_path, "r", encoding="utf8") 

        data_string=file2.read()
        #Then the phone number is    
        phone_number=data_string['data']['phone']    
        #    Then the name is
        name=data_string['data']['name']    
        #like above you can access all keys and value from dictionary