簡體   English   中英

將字符串變量作為字符串值存儲在字典中的最佳方法是什么?

[英]What is the best way to store a string variable as a string value in a dictionary?

將字符串變量作為字符串值存儲在字典中的最佳方法是什么?

在下面,我嘗試將一個名為authToken的字符串變量放置在名為header的字典中,以便可以使用請求模塊進行調用

if currentTime >= authExpTime:
        getAuthToken()
else:
        header = {'Content-Type':'application/json','Authorization':'OAUTH2 access_token=authToken'}
        print header
        for i in metricsCollected:
                callURL = apiURL + i + "?samepletime="
                print callURL
                apiResponse = requests.get(apiURL, headers=header)
                apiResponse.json()

您可以通過多種方式執行此操作。 有些比其他的更pythonic。 我可能會這樣做的方式是:

header = {
    'Content-Type': 'application/json',
    'Authorization': 'OAUTH2 access_token=%s' % (authToken)
}

這樣嗎

if currentTime >= authExpTime:
        getAuthToken()
else:
        header = {'Content-Type':'application/json','Authorization':'OAUTH2 access_token='}
        header["Authorization"] += authToken 
        print header
        for i in metricsCollected:
                callURL = apiURL + i + "?samepletime="
                print callURL
                apiResponse = requests.get(apiURL, headers=header)
                apiResponse.json()

暫無
暫無

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

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