0

I have the code as below,

    with open(Elastic_Secret_File) as f:
        yaml_data = yaml.safe_load(f)
        for key, value in dict(yaml_data["xxx"]["xxxx"]["xxxx"]["xxxx"]["xxxxx"]).items():
          Ela_User = value["clusterCredentials"]["username"]
          Ela_Pass = str(value["clusterCredentials"]["password"].replace("'", ""))
          #Ela_Pass = value["clusterCredentials"]["password"]
          Ela_Host = value["urls"]
          print (Ela_Pass)

es_secret_data = toml.load("./secrets/elasticsearch-password.toml")
es_secret_data['password'] = Ela_Pass
es_secret_data1 = ast.literal_eval(json.dumps(es_secret_data))
print (es_secret_data1)
with open('./secrets/elasticsearch-password.toml', 'w') as f:
    elastic_toml_string = toml.dump(es_secret_data1, f)

The Ela_Pass contains the password string with \ characters. The json adds extra backslash to the string which is not good as I want the final string without extra backslash.

for e.g

Ela_Pass = 1\n2\t3\a6
{'password': '1\n2\t3\\a6'}

I've tried to use .decode('string-escape) and .decode('unicode-escape') , but it works only for special sequences supported by Python.

Please help, how do I maintain my original string without any additional backslash ?

Brian THOMAS
  • 482
  • 1
  • 5
  • 19
Neelam Sharma
  • 99
  • 1
  • 2
  • 5

1 Answers1

2

You could remove dublicated backslashes with:

json.dumps(es_secret_data).replace('\\\\', '\\')

you need to escape the backslash character in python with a backslash. Thats why there is a quadruple-backslash.

But keep in mind that json adds those extra-backslashes for a reason. After replacing them, you will not be able to use json.reads() to get the original password. Json uses the backslash to escape the backslash. So a double-backslash in json stands for a single json-charakter

wuerfelfreak
  • 2,363
  • 1
  • 14
  • 29
  • looks like it only replaces the slashes for the special sequences. example: 1\t20\n30\g4567 {'password': '1\t20\n30\\g4567'} – Neelam Sharma Dec 28 '20 at 13:56
  • This is propably because `\t` and `\n` are escape sequences for `tablulator` and `newline` whereas \g is not. You could use replace('\\', '') to remove all backslashes alltogether. But this would again lead to loss of information as a linebreak would convert to a "n"-literal – wuerfelfreak Dec 28 '20 at 14:12
  • Also it passes unicode characters for \a and \b: 123\g4\b567 {'password': '123\\g4\x08567'} – Neelam Sharma Dec 28 '20 at 14:16
  • i would want \ as it is part of the string. so any way where i could maintain the original string ? – Neelam Sharma Dec 28 '20 at 14:17
  • I think, if you want to maintain the information, you should leave the extra backslashes where they are, as they are needed to tell json-decoder that the charakter in question is infact not a sepial-escape-sequenze. A double-backslash is just the json-representation of a single backslash-literal. Anything else would not be valid json. Or did I missunderstand your issue? – wuerfelfreak Dec 28 '20 at 14:24
  • Actualy, I'm trying to load the json output into the toml file as below,and the output includes the double backslash. Since the output file holds the password value, I'm afraid the application trying to use the password with double backslash might fail due to incorrect password Hence the need to maintain the string as as is or remove all the double backslashes. Output file: {'password': '123\\g4567'} – Neelam Sharma Dec 28 '20 at 17:04