简体   繁体   English

如何使用包含反斜杠的 json 的 .env 文件?

[英]How to use .env file with a json containing a backslash?

Background, I have a MSSQL server database that uses a specific instances, hence the connect string/engine to this database would look like this:背景,我有一个使用特定实例的 MSSQL 服务器数据库,因此到该数据库的连接字符串/引擎如下所示:

Engine(mssql+pyodbc://User:Password@servername\instance,5555/database?driver=ODBC+Driver+17+for+SQL+Server)引擎(mssql+pyodbc://User:Password@servername\instance,5555/database?driver=ODBC+Driver+17+for+SQL+Server)

Note the single back slash between servername and instance.请注意服务器名和实例之间的单个反斜杠。

My testing, with both as a string and a json types:我的测试,同时使用字符串和 json 类型:

Env File 1:环境文件 1:

myvar = '[{"A":"123","B":"456", "C":"A\B"}]'
myvar2 = abc
myvar3 = [{"A":"123","B":"456", "C":"A\B"}]

main.py File: main.py 文件:

from dotenv import load_dotenv
import os
import json

if __name__ == '__main__':
    load_dotenv('.env', override=True)
    myv2 = os.getenv('myvar2')
    myv = os.getenv('myvar')
    myv3 = os.getenv('myvar3')
    print(myv2)
    print(myv)
    print(myv3)
    print('Json Outputs')
    jmyv3 = json.loads(myv3)
    print(f"{jmyv3=}")
    jmyv = json.loads(myv)
    print(f"{jmyv=}")

Generates an error Invalid \escape for both the string variable in.env and the json variable in.env为字符串变量 in.env 和 json 变量 in.env 生成错误 Invalid \escape

Now, let's modify.env file to this:现在,让我们将 .env 文件修改为:

myvar = '[{"A":"123","B":"456", "C":"A\\B"}]'
myvar2 = abc
myvar3 = [{"A":"123","B":"456", "C":"A\\B"}]

The json variable returns but returns two back slashes and not one. json 变量返回但返回两个反斜杠而不是一个。

jmyv3=[{'A': '123', 'B': '456', 'C': 'A\\B'}]

And, the string version still errors with Invalid \escape而且,字符串版本仍然因 Invalid \escape 而出错

Now, for grins, I put three back slashes in.env现在,为了笑,我在 .env 中添加了三个反斜杠

myvar = '[{"A":"123","B":"456", "C":"A\\\B"}]'
myvar2 = abc
myvar3 = [{"A":"123","B":"456", "C":"A\\\B"}]

This time, string returns but with two back slashes:这一次,字符串返回但带有两个反斜杠:

jmyv=[{'A': '123', 'B': '456', 'C': 'A\\B'}]

And, the json generates an error Invalid \escape并且,json 生成错误 Invalid \escape

Is there anyway to get the 'C' part of the json string to just print a single back slash?有没有办法让 json 字符串的“C”部分只打印一个反斜杠? Like this:像这样:

[{'A': '123', 'B': '456', 'C': 'A\B'}]

And, these variables are stored in Airflow with base64 encoding which my cause another layer of uncertainty with back slashes.并且,这些变量存储在 Airflow 中,编码为 base64,这会导致另一层带有反斜杠的不确定性。

It is easy to get confused about the difference between the CONTENT of a string and the REPRESENTATION of that string.很容易混淆字符串的内容和该字符串的表示之间的区别。 Observe the following:请注意以下事项:

import json

myvar = '[{"A":"123","B":"456", "C":"A\\\\B"}]'
print(myvar)
jmyv3 = json.loads(myvar)
print(f"{jmyv3=}")
print(jmyv3[0]['C'])

Output: Output:

[{"A":"123","B":"456", "C":"A\\B"}]
jmyv3=[{'A': '123', 'B': '456', 'C': 'A\\B'}]
A\B

OK, let's break down what happened.好的,让我们分解一下发生的事情。 First:第一的:

myvar = '[{"A":"123","B":"456", "C":"A\\\\B"}]'

That string only contains two backslashes.该字符串仅包含两个反斜杠。 I have to type FOUR in order to make Python do what I want, but the string that is stored in myvar only has two backslashes.我必须键入 4 才能使 Python 执行我想要的操作,但存储在myvar中的字符串只有两个反斜杠。 You can see that in the output.您可以在 output 中看到。

Now, I load that string as JSON:现在,我将该字符串加载为 JSON:

jmyv3 = json.loads(myvar)

The string literally has two backslashes, which is what JSON needs if we want the resulting data to have one backslash.该字符串字面上有两个反斜杠,如果我们希望结果数据有一个反斜杠,这就是 JSON 所需要的。 So, when I print that one value, jmyv3[0]['C'] , as you see THAT string only has one backslash.因此,当我打印那个值jmyv3[0]['C']时,如您所见,该字符串只有一个反斜杠。

So, the answer to your question depends on whether that file is being interpreted as JSON, or interpreted as Python code.因此,您问题的答案取决于该文件是被解释为 JSON 还是被解释为 Python 代码。 It if is Python code, then you need 4 backslashes to produce 1 backslash.如果是 Python 代码,则需要 4 个反斜杠才能产生 1 个反斜杠。

Having said all that, assuming that is on Windows, EVERY Windows API will accept forward slashes anywhere it expect backslashes, so you could just skip all of this and use a forward slash.说了这么多,假设它在 Windows 上,每个 Windows API 都会在它期望反斜杠的任何地方接受正斜杠,所以你可以跳过所有这些并使用正斜杠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM