我有如下代码
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)
Ela_密码包含带\个字符的密码字符串。 json向sting添加了额外的反斜杠,这不好,因为我希望最后的字符串没有额外的反斜杠
例如
Ela_Pass = 1\n2\t3\a6
{'password': '1\n2\t3\\a6'}
我曾尝试使用.decode('string-escape')和.decode('unicode-escape'),但它只适用于Pyton支持的特殊序列
请帮助,我如何保持我的原始字符串没有任何额外的反斜杠
您可以使用以下方法删除重复的反斜杠:
您需要用反斜杠转义python中的反斜杠字符。这就是为什么有四个反斜杠
但请记住,json添加这些额外的反斜杠是有原因的。替换后,您将无法使用
json.reads()
获取原始密码。Json使用反斜杠来转义反斜杠。所以json
中的双反斜杠代表单个json字符相关问题 更多 >
编程相关推荐