【发布时间】:2020-08-29 20:35:49
【问题描述】:
我有一个 GraphQL 查询字符串
query = """
{
scripts(developers: "1") {
...
...
}
}
"""
问。如何使用 Python 字符串格式化技术更改 developers 的值?
到目前为止我已经尝试过,
1.使用 f-string
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'
【问题讨论】:
标签: python python-3.x string-formatting f-string