我有一个GraphQL查询字符串
query = """
{
scripts(developers: "1") {
...
...
}
}
"""
问:如何developers
使用Python字符串格式化技术更改的值?
到目前为止我尝试过的
1.使用f弦
In [1]: query = f"""
...: {
...: scripts(developers: "1") {
...:
...: ...
...: ...
...: }
...: }
...: """
File "<fstring>", line 2
scripts(developers: "1") {
^
SyntaxError: invalid syntax
2.使用.format()
方法
In [2]: query = """
...: {
...: scripts(developers: "{dev_id}") {
...:
...: ...
...: ...
...: }
...: }
...: """
...:
...: query.format(dev_id=123)
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-2-058a3791fe41> in <module>
9 """
10
---> 11 query.format(dev_id=123)
KeyError: '\n scripts(developers'
使用double-curly-brace而不是single-curly-brace在f字符串中写一个字面大括号:
dev_id = 1
query = f"""
{{
scripts(developers: "{dev_id}") {{
...
...
}}
}}
"""
print(query)
# {
# scripts(developers: "1") {
#
# ...
# ...
# }
# }
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句