在 python 中,如何在 api 端点的 { 之前添加 $ 符号

In python, How can i add a $ symbol just before { in the api endpoint

我需要在每一行的 { 之前添加一个 $ 符号。我怎样才能在 python、

中做到这一点

问题:使用 python 我正在从 JSON 文件读取所有 API 端点,然后再传递那些 API 端点,我需要附加一个 $ 符号就在左括号 {

之前

下面是从 JSON 文件中读取 API 端点名称并打印的代码。

import json
with open("example.json", "r") as reads: # Reading all the API endpoints  from json file.
    data = json.load(reads)
    print(data['paths'].items())
    for parameters, values in data['paths'].items():
        print(parameters)

从上面的代码中,我需要进一步实现在打印之前在 { 旁边添加一个 $ 符号。

下面的列表是我使用 python 读取 json 文件得到的:

/API/{id}/one
/{two}/one/three
/three/four/{five}

预计是:

/API/${id}/one
/${two}/one/three
/three/four/${five}

您可以使用 re 库。

for parameters, values in data['paths'].items():
     print(re.sub('{', '${', parameters))

有关 re 的更多信息,请浏览文档。 https://docs.python.org/3/library/re.html,这是一个非常有用的模块。

你可以使用 .replace().

>>> obj="""
... /API/{id}/one
... /{two}/one/three
... /three/four/{five}
... """
>>> newobj = obj.replace('{','${')
>>> print(newobj)

/API/${id}/one
/${two}/one/three
/three/four/${five}