Python datetime TypeError:在字符串格式化过程中未转换所有参数
我确信有更简单的方法可以做到这一点,但我不确定为什么我总是遇到打字错误Python datetime TypeError:在字符串格式化过程中未转换所有参数,python,string,datetime,string-formatting,netdna-api,Python,String,Datetime,String Formatting,Netdna Api,我确信有更简单的方法可以做到这一点,但我不确定为什么我总是遇到打字错误 import datetime getdate = datetime.date.today() thirty = datetime.timedelta(days=30) last_month = getdate - thirty print json.dumps(api.get_zone_stats(3, "daily", "%s", "%s" %(last_month, getdate)))
import datetime
getdate = datetime.date.today()
thirty = datetime.timedelta(days=30)
last_month = getdate - thirty
print json.dumps(api.get_zone_stats(3, "daily", "%s", "%s" %(last_month, getdate)))
回溯(最近一次呼叫最后一次):
类型错误:并非所有参数都在字符串格式化过程中转换问题在于:
api.get_zone_stats(3, "daily", "%s", "%s" %(last_month, getdate))
%
运算符仅对上一个字符串起作用:
"%s" %(last_month, getdate)
一个%s
有两个变量
试试这个:
api.get_zone_stats(3, "daily", "{0}".format(last_month), "{0}".format(getdate))
或者这个:
api.get_zone_stats(3, "daily", str(last_month), str(getdate))
在最后一行中,您尝试使用两个值格式化字符串,但该字符串仅包含一个填充点:
print json.dumps(api.get_zone_stats(3, "daily", "%s", "%s" %(last_month, getdate)))
你的意思是说以下内容吗
print json.dumps(api.get_zone_stats(3, "daily", str(last_month), str(getdata)))