假設我有多個檔案,例如 file1、file2 和 file3,并且包含以下資料:
檔案 1:
{"status":"succes","message":"User Found","error_code":"0","data":{"phone":0,"name":"Sanju Mehra","gender":"","tob":"","dob":"","pob":"","email":"[email protected]"}
檔案2:
{"status":"succes","message":"User Found","error_code":"0","data":{"phone":0,"name":"Anil Kumar","gender":"","tob":"","dob":"","pob":"","email":"[email protected]"}
檔案 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":"[email protected]"}
我想從這些 multipe 檔案中提取姓名和電話號碼,我實際上想在其中提取電話號碼的資料和在 name 引數上輸入的資料。
你能告訴我可能的方法嗎?
uj5u.com熱心網友回復:
檔案中的資料看起來像字典格式。那么答案如下
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/346596.html
上一篇:合并來自2個陣列的資料并重命名鍵
下一篇:使用特定格式的特征標記文本
