I am a newbie in python. I want to write a for loop to iterate the index in order to pull the data.
Here is my code:
url90=[]
for i in range(-90,0):
url90.append('http://staging.foglogic.com/api/v1/index.php/accounts/34/reports/bcjobs?cmd=json&datestr=today&**index=i**&filter={}&filterOverride=0&su=1')
I want index=i which from range(-90,0), however the python consider my i as a string instead of a integer.
my result give me 90 identical url :
'http://staging.foglogic.com/api/v1/index.php/accounts/34/reports/bcjobs?cmd=json&datestr=today&index=i&filter={}&filterOverride=0&su=1'
Is there anyone can help me to solve the problem?
Thank you!
答案 0 :(得分:1)
如果您认为i
变量将自动用于填充列表的字符串中,那么您错了。它不起作用。使用.format
:
url90=[]
for i in range(-90,0):
url90.append('http://staging.foglogic.com/api/v1/index.php/accounts/34/reports/bcjobs?cmd=json&datestr=today&index={0}&filter={{}}&filterOverride=0&su=1'.format(i))
print("\n".join(url90))
请参阅Python demo
请注意,格式字符串中的文字{
和}
必须加倍(请参阅index={0}
)。 {0}
是i
变量的占位符(请参阅filter={{}}
),该变量是该方法的第一个也是唯一的参数。