Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

json - How can I split the path by (".") using python?

-----------------------mapper-------------------
"contact_information":{
      "person_name":{
         "FormattedName":"some name"
      }
   }   
--------------------current data---------------
client_profile_data = {
   "contact_information":{
      "person_name":{
         "FormattedName":"Abu DND Md"
      }
   } 
}
---------------------changed data------------
profile_data = {
   "contact_information":{
      "person_name":{
         "FormattedName":"Abu DND" 
      }
   } 
}

I need to get the changes of "FormattedName(Field)"  between client_profile_data & profile_data. So I wrote some function in "helper.py"
------------------------helper.py------------------

 PROFILE_FEEDBACK_MAPPINGS  = {
    'FormattedName': {
        'type': 'nested',
        'parent_name': "person_name",
        'path': "contact_information.person_name.FormattedName"
       }
    }


def find_diff(client_profile_data, profile_data): 
    result = []
    for key, value in PROFILE_FEEDBACK_MAPPINGS.items():
        if value['type'] == 'nested':
            try:
                if client_profile_data[value['path'][0][1]][key] != profile_data[value['path'][0][1]][key]:
                    result.append({
                        
                        'current': profile_data[value['parent_name']][key],
                        'changed': client_profile_data[value['parent_name']][key],
                        
                    })
            except Exception:
                continue
    return result

----------------Expected output------------------- changed: "Abu DND" current: "Abu DND Md" -----------------Actual output--------- getting none

Can anyone help me? I need a changes from client_profile_data and profile_data so that I define a function initially which will check the type and after that I want to split the path bcz(contact_information.person_name.FormattedName) will give second if condition will get the differences so that differences will be appending to result. I tried in this way but not working, please help me.

question from:https://stackoverflow.com/questions/66061057/how-can-i-split-the-path-by-using-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Not sure about what you are looking for but with minimal changes of your code, a solution coud be :

def find_diff(client_profile_data, profile_data): 
    result = []
    for key, value in PROFILE_FEEDBACK_MAPPINGS.items():
        if value['type'] == 'nested':
            try:
                split_path = value['path'].split(".")
                client_name = client_profile_data[split_path[0]][split_path[1]][key]
                profile_name = profile_data[split_path[0]][split_path[1]][key]

                if client_name != profile_name:
                    result.append({
                        'current': profile_data[split_path[0]][value['parent_name']][key],
                        'changed': client_profile_data[split_path[0]][value['parent_name']][key],
                        
                    })
            except Exception:
                continue
    return result

You forgot to "split" the path to use it as "keys" for your dictionnaries.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...