简体   繁体   English

Python:创建带有日期的字典时语法无效

[英]Python: Invalid syntax when creating a dictionary with dates

I enter a date followed by time using Python and flask.我使用 Python 和 Flask 输入一个日期,然后输入时间。 What is the problem?问题是什么?

from flask import Flask,render_template,request,jsonify
import datetime

app=Flask(__name__)


calendrier=[
{
    'start':2013-9-22 16:19:43,
    'end':2015-8-11 13:30:00
}}

As the title of your question asks for why is there an invalid syntax error then that is cause you are using a curly } brace to close your calendrier list instead of a square ] bracket as well as the way you are storing the dates.由于您的问题的标题询问为什么存在无效的语法错误,那是因为您使用花括号}括号来关闭calendrier列表而不是方]括号以及您存储日期的方式。

You should either store the date in string format as suggested by S3DEV or as you have imported the datetime module use a datetime object to store the date which is recommended for you.您应该按照S3DEV 的建议以字符串格式存储日期,或者当您导入 datetime 模块时,使用 datetime 对象来存储为您推荐的日期。 Try:尝试:

from flask import Flask,render_template,request,jsonify
import datetime

app=Flask(__name__)


calendrier=[
{
    'start': datetime.datetime(2013,9,22,16,19,43),
    'end': datetime.datetime(2015,8,11,13,30,0)
}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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