Search code examples
python-3.xcharacter

new line character creating issue in replacing key-value pair in python


in a piece of code i need to replace the environment - example :

txt = "defaultValue":  "dev"

Now i want to replace the value "dev" with "uat"

it is working fine until new line is introduced. so

"defaultValue":  

"dev"

is not working. what will be the solution?


Solution

  • If the problem is only the '\n' in the uat means we can remove it using this code.

    txt = {"defaultValue": "dev"}
    print("Initial:     ", txt)
    new_val = "\nuat\n"
    txt['defaultValue'] = new_val
    print("after update:",txt)
    txt["defaultValue"] = new_val.replace("\n", "")
    print("Final:       ", txt)
    
    # Output:
    # Initial:      {'defaultValue': 'dev'}
    # after update: {'defaultValue': '\nuat\n'}
    # Final:        {'defaultValue': 'uat'}