如何在Python代码中使用JSON中的替代占位符?

huangapple go评论70阅读模式
英文:

How to use replace holder in json with python code?

问题

我有一个问题,想要用Python更新JSON脚本中的占位符。

以下是代码部分,我将不进行翻译:

json_str = '''<Execute xmlns="urn:schemas-microsoft-com:xml-analysis">
				  <Statement>
				  <DatabaseName>{database}</DatabaseName>
					<![CDATA[
					{{
                      "create": {{
                        "object": {{
                          "database":"{{{database}}}"
                        }}
                        }}
                      }}
					]]>
				</Statement>
		</Execute>'''

replaced_str = json_str.format(database='testDB')
print(replaced_str)

输出结果如下:

<Execute xmlns="urn:schemas-microsoft-com:xml-analysis">
				  <Statement>
				  <DatabaseName>testDB</DatabaseName>
					<![CDATA[
					{
                      "create": {
                        "object": {
                          "database":"{testDB}"
                        }
                        }
                      }
					]]>
				</Statement>
		</Execute>

有谁了解如何更改大括号的转义以获取期望的输出,从:
"database":"{testDB}""database":"testDB"

谢谢!

英文:

I have a question to update json script with replace holder with python.

The code:

json_str = &#39;&#39;&#39;
&lt;Execute xmlns=&quot;urn:schemas-microsoft-com:xml-analysis&quot;&gt;
				  &lt;Statement&gt;
				  &lt;DatabaseName&gt;{database}&lt;/DatabaseName&gt;
					&lt;![CDATA[
					{{
                      \&quot;create\&quot;: {{
                        \&quot;object\&quot;: {{
                          \&quot;database\&quot;:\&quot;{{{database}}}\&quot;
                        }}
                        }}
                      }}
					]]&gt;
				&lt;/Statement&gt;
		&lt;/Execute&gt;&#39;&#39;&#39;

replaced_str = json_str.format(database=&#39;testDB&#39;)
print(replaced_str)

The output is:

 &lt;Execute xmlns=&quot;urn:schemas-microsoft-com:xml-analysis&quot;&gt;
                                  &lt;Statement&gt;
                                  &lt;DatabaseName&gt;testDB&lt;/DatabaseName&gt;
                                        &lt;![CDATA[
                                        {
                      &quot;create&quot;: {
                        &quot;object&quot;: {
                          &quot;database&quot;:&quot;{testDB}&quot;
                        }
                        }
                      }
                                        ]]&gt;
                                &lt;/Statement&gt;
                &lt;/Execute&gt;

Anybody is familiar about how to change the curly brace escape to get the expected output from:
"database":"{testDB}" to "database":"testDB" ?

Thanks

答案1

得分: 2

json_str = &#39;&#39;&#39;
        &lt;Execute xmlns=&quot;urn:schemas-microsoft-com:xml-analysis&quot;&gt;
            &lt;Command&gt;
                  &lt;Statement&gt;
                  &lt;DatabaseName&gt;{database}&lt;/DatabaseName&gt;
                    &lt;![CDATA[
                    {
                      &quot;create&quot;: {
                        &quot;object&quot;: {
                          &quot;database&quot;:&quot;{database}&quot;
                        }
                        }
                      }
                    ]]&gt;
                &lt;/Statement&gt;
            &lt;/Command&gt;
            &lt;Properties /&gt;
        &lt;/Execute&gt;&#39;&#39;&#39;
replaced_str = json_str.format(database=&#39;testDB&#39;)
print(replaced_str)
英文:

Remove two of {}: From {{{database}}} to {database}:

json_str = &#39;&#39;&#39;
        &lt;Execute xmlns=&quot;urn:schemas-microsoft-com:xml-analysis&quot;&gt;
            &lt;Command&gt;
                  &lt;Statement&gt;
                  &lt;DatabaseName&gt;{database}&lt;/DatabaseName&gt;
                    &lt;![CDATA[
                    {{
                      \&quot;create\&quot;: {{
                        \&quot;object\&quot;: {{
                          \&quot;database\&quot;:\&quot;{database}\&quot;
                        }}
                        }}
                      }}
                    ]]&gt;
                &lt;/Statement&gt;
            &lt;/Command&gt;
            &lt;Properties /&gt;
        &lt;/Execute&gt;&#39;&#39;&#39;
replaced_str = json_str.format(database=&#39;testDB&#39;)
print(replaced_str)

Output:


        &lt;Execute xmlns=&quot;urn:schemas-microsoft-com:xml-analysis&quot;&gt;
            &lt;Command&gt;
                  &lt;Statement&gt;
                  &lt;DatabaseName&gt;testDB&lt;/DatabaseName&gt;
                    &lt;![CDATA[
                    {
                      &quot;create&quot;: {
                        &quot;object&quot;: {
                          &quot;database&quot;:&quot;testDB&quot;
                        }
                        }
                      }
                    ]]&gt;
                &lt;/Statement&gt;
            &lt;/Command&gt;
            &lt;Properties /&gt;
        &lt;/Execute&gt;

In fact, you can also replace \&quot; by &quot;.

huangapple
  • 本文由 发表于 2023年2月19日 06:19:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定