httpリクエストのURLを組み立てています
baseurl = 'http://..'
action = 'mef'
funcId = 100
year = 2018
month = 1
url = '{}?action={}&functionId={}&yearMonth={}{}'.format(baseurl, action, funcId, year, month)
私を悩ませているのは、月番号が10未満の場合、0で埋める必要があることです。フォーマットする唯一の変数である場合、数字を埋める方法を知っています。
'{0:02d}'.format(month) # returns: 01
私がこれを試してみると:
'{}?action={}&functionId={}&yearMonth={}{0:02d}'.format(baseurl, action, funcId, year, month)
エラーが発生します:
ValueError: cannot switch from automatic field numbering to manual field specification
他の角かっこにはどのタイプの変数が期待されるかが示されていないためだと思いましたが、文字列を指定する文字がわかりません。
これはうまくいくはずです
url = '{}?action={}&functionId={}&yearMonth={}{num:02d}'.format(baseurl, action, funcId, year, num=month)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加