-3

Suppose i have multiple files such as file1 file2 and file3 and consists of following data:

file1:

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

file2:

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

file3:

{"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"}

I'd like to extract name and phone number from these multipe file where i actually want to extract the data of a phone number and the data entered on name parameter.

Could you please tell me the possible ways with which i could do that?

LeoE
  • 2,054
  • 1
  • 11
  • 29
D - Lord
  • 3
  • 2

1 Answers1

0

The data in a file is looking like a dictionary format. Then the answer is as follows

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
Satya Chandra
  • 728
  • 2
  • 12
  • 26
  • hello, thank you for writing the code i seem to get the following error in when i run this in pycharm. Statement expected, found Py:DEDENT also could you please tell me what shall i do if i have 2000+ files and would like to transfer all the extracted data to a new file? THANKS! – D - Lord Nov 03 '21 at 08:56
  • Is it all files having same syntax of data and each file having only one dict? For your Py:DEDNT solution at https://stackoverflow.com/questions/47776315/error-statement-expected-found-py-dedent – Satya Chandra Nov 03 '21 at 09:01
  • Yes, all the files seem to have same syntax just the data may vary. – D - Lord Nov 03 '21 at 09:04
  • thnk u, can u also please write the code for multiple file they have numbers from 0 to 4000 as their file names – D - Lord Nov 03 '21 at 09:08
  • My answer is updated .Accept it if you got the answer. – Satya Chandra Nov 03 '21 at 09:25