我尝试通过重复12次相同的请求来对API请求进行循环:
这是请求的有效负载部分,这是有效的:
payload="{\n\t\"filter\": {\n \"year\":2020,\n \"month\":10,\n\t\t\"customer_id\":52\n\t},\n \"sort\":{\"_id.date\":1}\n}"我的目标是通过在月份后添加循环变量"mes“来格式化有效负载字符串。据我所知,我试图使用:
for mes in range(0,12):
payload="{\n\t\"filter\": {\n \"year\":2020,\n \"month\":
{mes},\n\t\t\"customer_id\":52\n\t},\n \"sort\":{\"_id.date\":1}\n}".format(mes=mes)但不工作,我如何格式化它,以便我可以循环通过有效负载?可能的解决方案是什么?
谢谢
发布于 2021-01-14 22:04:16
当您使用.format()时,不能在字符串中包含{},因为它们将被识别为在.format()中放置字符串的位置。在不打算使用格式化字符串的地方,您需要将它们中的每一个加倍:
for mes in range(0,12):
payload="{{\n\t\"filter\": {{\n \"year\":2020,\n \"month\"{mes},\n\t\t\"customer_id\":52\n\t}},\n \"sort\":{{\"_id.date\":1}}\n}}".format(mes=mes)简而言之,'{a} {something else}'.format(a=1)不起作用,但'{a} {{something else}}'.format(a=1)起作用,并被打印为'1 {something else}'
https://stackoverflow.com/questions/65720040
复制相似问题