Home > Net >  Value is getting changed after assign it to dictionary in Python
Value is getting changed after assign it to dictionary in Python

Time:12-25

I have created a inputjson variable and then replacing "'" to '\a"' and creating a dictionary and assigning to json key but the output of newjson variable and dictionary key value is different. In dictionary output / is replaced by //. Can anyone help me with the reason behind this issue.

inputjson = {"project_path": "Store_Execution_Analytics/DSCI/Tech","project_path_pqm": "Store_Execution_Analytics/DSCI/Tech/","project_path_powerbi": "Store_Execution_Analytics/DSCI/Tech","input_db_name": "rtm_storeexecution_pqm"}
print("The existing json value is: "   str(inputjson))

newjson = str(inputjson).replace(r"'", r'\a"')
print("The new json value is: "   str(newjson))
dict1={}
dict1['json'] = newjson
print(dict1)

output

inputjson = {'gen': 'UAT', 'gen2': 'eu', 'json': '{"project_path": "Store_Execution_Analytics/DSCI/Tech","project_path_pqm": "Store_Execution_Analytics/DSCI/Tech/"}'}

How can I acheive below output in python?

output = {'gen': 'UAT', 'gen2': 'eu', 'json': '{\a"project_path\a": \a"Store_Execution_Analytics/DSCI/Tech\a",\a"project_path_pqm\a": \a"Store_Execution_Analytics/DSCI/Tech/\a"}'}

CodePudding user response:

In python, the \ character is used to escape characters that otherwise have a special meaning, such as newline \n, backslash itself \\, or the quote character \'\". See more at https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals.

Now in the first print (i.e. print("The new json value is: " str(newjson))), you pass a string to the print function. The print function then displays the string in the console with the escaped characters resolved (a newline char will appear as a true line separator instead of the \n character, etc.).

In the second print (i.e. print(dict1)), the concerned string is a key in the dictionary dict1. Printing a dictionary does not resolve escape characters as it prints the representation of its keys and values. This is why you see the \\ escaped.

CodePudding user response:

This happens because you're putting the string into a dictionary and then printing the entire dictionary. In order for python to represent the dictionary value correctly it has to escape the \a's. You'll see though if you print the value specifically associated with the json key in dict1 it prints as expected.

inputjson = {"project_path": "Store_Execution_Analytics/DSCI/Tech","project_path_pqm": "Store_Execution_Analytics/DSCI/Tech/","project_path_powerbi": "Store_Execution_Analytics/DSCI/Tech","input_db_name": "rtm_storeexecution_pqm"}
print("The existing json value is: "   str(inputjson))

newjson = str(inputjson).replace(r"'", r'\a"')
print("The new json value is: "   str(newjson))
dict1={}
dict1['json'] = newjson
print(dict1['json']) // this prints as expected

Here's an example of this working. https://www.mycompiler.io/view/GmTFEYm

  • Related