我一直在嘗試弄清楚如何添加從 Locust 上的先前“任務”捕獲的變數,并將變數添加到新的任務GET請求 URL。
下面的變數 ,portid假設在 get 請求結束時繼續:/portfolios/portid
示例 1:
@task
def PortPage(self):
get = self.client.get("/portfolios/" portid, data=data, headers=header)
錯誤:
TypeError: can only concatenate str (not "int") to str
示例 2:
我也嘗試過使用%標志,因為我讀過它以前有效,但不適用于我的例子..
@task
def PortPage(self):
get = self.client.get("/portfolios/" % portid, data=data, headers=header)
錯誤:
TypeError: not all arguments converted during string formatting
任何幫助顯然表示贊賞。
uj5u.com熱心網友回復:
您只是在尋找正常的字串格式!
>>> portid = 100
>>> f"/portfolios/{portid}"
'/portfolios/100'
問題很簡單,當您嘗試將數字添加到字串時,Python 不會猜測會發生什么!
您可以根據需要從幾個選項中進行選擇,但 f-string 變體可能更適合您的情況
>>> "/portfolios/" portid # fails with the error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
>>> "/portfolios/" str(portid) # effectively simplest
'/portfolios/100'
>>> f"/portfolios/{portid}" # modern
'/portfolios/100'
>>> "/portfolios/{}".format(portid) # obvious
'/portfolios/100'
>>> "/portfolios/%i" % portid # considered old, will do some coercions
'/portfolios/100'
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/472096.html
