簡體   English   中英

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

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

我需要在每行的 { 之前添加一個 $ 符號。 我怎么能在python中做到這一點,

問題:在傳遞這些 API 端點之前,我正在使用 python 從 JSON 文件中讀取所有 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}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM