1

I have a python dictionary and I would like to find and replace part of the characters in the values of the dictionary. I'm using python 2.7. My dictionary is

data1 = {'customer_order': {'id': '20'},
         'patient': 
             {'birthdate': None,
             'medical_proc': None,
             'medical_ref': 'HG_CTRL12',
             'name': 'Patient_96',
             'sex': None},
          'physician_name': 'John Doe'
         }

I would like to change the underscore to backslash underscore only in the values of the dictionary, in this case only for Patient_96 and HG_CTRL12.

I would like to change it to the following:

 data1 = {'customer_order': {'id': '20'},
          'patient': 
              {'birthdate': None,
               'medical_proc': None,
               'medical_ref': 'HG\_CTRL12',
               'name': 'Patient\_96',
               'sex': None},
          'physician_name': 'John Doe'
         }

Thank you for your help

5
  • What should Patient_96 be changed to? Your question says, change '_' to '_'. I do not see the difference? Commented Sep 17, 2014 at 17:02
  • Sorry but the underscore were omitted from my question Commented Sep 17, 2014 at 17:07
  • It may make it more clear if you post a "before and after" of the data. Commented Sep 17, 2014 at 17:07
  • @JoanaRosadoCoelho Even the current edit does not make sense. What does "I would like to change the underscore to \underscore" mean? Commented Sep 17, 2014 at 17:08
  • I'm usually all about questions being as precise as possible, but what exactly the replacement string is doesn't really matter here, does it? The tricky part is recursively dealing with nested dictionaries. Commented Sep 17, 2014 at 17:11

2 Answers 2

2

This function recursively replaces the underscore in the values of the dictionary with replace_char:

def replace_underscores(a_dict, replace_char):
    for k, v in a_dict.items():
        if not isinstance(v, dict):
            if v and '_' in v:
                a_dict[k] = v.replace('_', replace_char)
        else:
            replace_underscores(v, replace_char)

More on isinstance() here.

Sign up to request clarification or add additional context in comments.

Comments

0
>>> for i in data1:
...     if type(data1[i]) is str:
...             if data1[i]:
...                     data1[i] = data1[i].replace('_','\_')
...     elif type(data1[i]) is dict:
...             for j in data1[i]:
...                     if data1[i][j]:
...                             data1[i][j] = data1[i][j].replace('_','\_')
... 
>>> 
>>> 
>>> data1
{'physician_name': 'John Doe', 'customer_order': {'id': '20'}, 'patient': {'medical_ref': 'HG\\_CTRL12', 'medical_proc': None, 'name': 'Patient\\_96', 'birthdate': None, 'sex': None}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.