简体   繁体   中英

python triple quote string adding multiple variables

I am using python 2.7. I have a triple quote string that I want to add 4 variables into. That string will then be used in a post request that our REST API will handle. Below is the string, it is shortened bc it is over 500 line long:

curDate = str(datetime.datetime.now().year)+"/"+str(datetime.datetime.now().month)+"/"+str(datetime.datetime.now().day)\
          +" "+str(datetime.datetime.now().hour)+":"+str(datetime.datetime.now().minute)+":"\
          +str(datetime.datetime.now().second)

...

payload = '''
   ...
"CaptureTime": %(captureTime),
   ...
"dataTime1": %(dataTime1)
   ...
"dataTime2": %(dataTime2)
   ...
"dataTime3": %(dataTime3)
   ...
"dataTime4": %(dataTime4)
   ...
''' % dict(captureTime=curDate, dataTime1=curDate, dataTime2=curDate, dataTime3=curDate, dataTime4=curDate)

This is the error I receive:

Traceback (most recent call last):
  File "/xxx/xxxx/xxxxxx/Rest/Post.py", line 6130, in <module>
    ''' % {'captureTime':curDate, 'dataTime1':curDate, 'dataTime2':curDate, 'dataTime3':curDate, 'dataTime4':curDate}
ValueError: unsupported format character ',' (0x2c) at index 204

Thank you for any and all help!

The proble is you use:

%(captureTime),

Instead of qualifying the type (I assume you are trying to qualify as string):

%(captureTime)s,

And so the comma is not recognized as type qualifier for %.

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