Python Dictionary Double Backslash Issue

Viewed 21

I have a Dictionary in which I am getting double backslash.

Here is the Dictionary:

ben = {
   "customer_full_nm": "D\\ NONE",
   "customer_type": "123",
   "party_key": 345,
   "party_rel_key": "11"
  }

As you can see, customer_full_nm has double backslash (\\). Now I want to convert this double backslash to single backslash for which I have created a function.


def transform_record_dict(record_dict: dict) -> dict:
    temp_dict1 = {}
    for k,v in record_dict.items():
        if k == 'customer_full_nm':
            value = v.replace(r'\\',chr(92))
            temp_dict1[k] = '{0}'.format(value)
        else:
            temp_dict1[k] = v
    return temp_dict1

I am calling this function using command: transform_record_dict(ben)

Below is the output of the function:

{'customer_full_nm': 'D\\ NONE', 'customer_type': '123', 'party_key': 345, 'party_rel_key': '11'}

As you can see, both variables 'v' and 'value' has single backslash but when inside the dictionary, single backslash is getting converted to double backslash. The requirement is to get single backslash inside the dictionary.

I am inserting this complete dictionary in one of the NoSQL(DynamoDB) Database. The dictionary which is getting inserted is :

{'customer_full_nm': 'D\\ NONE', 'customer_type': '123', 'party_key': 345, 'party_rel_key': '11'}

But the desired result is :

{'customer_full_nm': 'D\ NONE', 'customer_type': '123', 'party_key': 345, 'party_rel_key': '11'}

Would be great if anyone can help on this. Thank you in Advance.

0 Answers
Related