简体   繁体   中英

Invalid format string in Python

I get the invalid format string error when I try to run the below code (last line), not sure where I am missing the point:

  import datetime
  DAYS = 2
  SINCE = datetime.datetime.now() - datetime.timedelta(days=DAYS)
  params = "?fields=feed.since(" + SINCE.strftime("%s") + ").limit(1),name,updated_time&"

Any suggestions would be much appreciated !!

You have to use "%S" because "%s" is not defined in the method you called : https://docs.python.org/2/library/datetime.html#datetime.strftime

import datetime
DAYS = 2
SINCE = datetime.datetime.now() - datetime.timedelta(days=DAYS)
params = "?fields=feed.since(" + SINCE.strftime("%S") + ").limit(1),name,updated_time&"

You should add what format you need for your application.

这实际上取决于适合您的格式,但如果您需要使用时间戳:

int(time.mktime(SINCE.timetuple()))

it works fine for me (Python 2.7). If it is part of a query and it is failing on that part, maybe you can use another date format like:

params = "?fields=feed.since(" + SINCE.strftime("%Y-%m-%d %H:%M:%S") + ").limit(1),name,updated_time&"

Please note that capital "S", will give you the seconds of that datetime object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM