我正在使用python 2.7。我有一个三重引号字符串,我想添加4个变量。然后,该字符串将用于我们的REST API将处理的post请求中。下面是字符串,它缩短了bc超过500行:
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)
这是我收到的错误:
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
感谢您的帮助!
答案 0 :(得分:2)
你使用的问题是:
%(captureTime),
而不是限定类型(我假设您正在尝试限定为字符串):
%(captureTime)s,
因此,逗号不会被识别为%的类型限定符。