英文:
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 = '''
<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)
The output is:
<Execute xmlns="urn:schemas-microsoft-com:xml-analysis">
<Statement>
<DatabaseName>testDB</DatabaseName>
<![CDATA[
{
"create": {
"object": {
"database":"{testDB}"
}
}
}
]]>
</Statement>
</Execute>
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 = '''
<Execute xmlns="urn:schemas-microsoft-com:xml-analysis">
<Command>
<Statement>
<DatabaseName>{database}</DatabaseName>
<![CDATA[
{
"create": {
"object": {
"database":"{database}"
}
}
}
]]>
</Statement>
</Command>
<Properties />
</Execute>'''
replaced_str = json_str.format(database='testDB')
print(replaced_str)
英文:
Remove two of {}
: From {{{database}}}
to {database}
:
json_str = '''
<Execute xmlns="urn:schemas-microsoft-com:xml-analysis">
<Command>
<Statement>
<DatabaseName>{database}</DatabaseName>
<![CDATA[
{{
\"create\": {{
\"object\": {{
\"database\":\"{database}\"
}}
}}
}}
]]>
</Statement>
</Command>
<Properties />
</Execute>'''
replaced_str = json_str.format(database='testDB')
print(replaced_str)
Output:
<Execute xmlns="urn:schemas-microsoft-com:xml-analysis">
<Command>
<Statement>
<DatabaseName>testDB</DatabaseName>
<![CDATA[
{
"create": {
"object": {
"database":"testDB"
}
}
}
]]>
</Statement>
</Command>
<Properties />
</Execute>
In fact, you can also replace \"
by "
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论