web.py datetime Invalid format string

本文探讨了在使用web.py框架时遇到的一个日期格式化问题,即strftime函数不支持%e参数,而改为使用%d参数解决了此问题,并提供了修改建议。

那个blog的小程序,今天再运行的时候出错了。看了一下记录:

File “C:\Python27\lib\site-packages\web.py-0.36-py2.7.egg\web\utils.py”, line 908, in da
testr
out = then.strftime(‘%B %e’) # e.g. ‘June 13′
ValueError: Invalid format string

浏览器提示的错误是:
<type ‘exceptions.ValueError’> at /
Invalid format string

Python C:\Python27\lib\site-packages\web.py-0.36-py2.7.egg\web\utils.py in datestr, line 907
Web GET http://localhost:8080/

C:\Python27\lib\site-packages\web.py-0.36-py2.7.egg\web\utils.py in datestr

C:\Python27\lib\site-packages\web.py-0.36-py2.7.egg\web\utils.py in datestr
    deltadays = abs(deltaseconds) // oneday
    if deltaseconds < 0: deltadays *= -1 # fix for oddity of floor
    if deltadays:
        if abs(deltadays) < 4:
            return agohence(deltadays, 'day')
        out = then.strftime('%B %e') # e.g. 'June 13' ...
        if then.year != now.year or deltadays < 0:
            out += ', %s' % then.year
        return out
    if int(deltaseconds):
        if abs(deltaseconds) > (60 * 60):
▼ Local vars
Variable	Value
agohence	
<function agohence at 0x01495FB0>
delta	datetime.timedelta(5, 1347, 859000)
deltadays	5
deltaseconds	433347
now	datetime.datetime(2012, 6, 4, 22, 6, 4, 859000)
oneday	86400
then	datetime.datetime(2012, 5, 30, 21, 43, 37)
templates\index.html in __template__
        from $datestr(post.posted_on)  ...

问题是出在了strftime函数上。查了一下这个函数,官方网站8.1.7. strftime() and strptime() Behavior上不支持%e这个参数。但是以前的却支持Python time strftime() Function,这里还有一个讨论的Why %e not in time.strftime directives? 。

%d Day of the month as a decimal number [01,31].

%e – day of the month (1 to 31)

所以,根据Python的2.7.3版本,修改web\utils.py”, line 908,%e为%d即可。

        #out = then.strftime('%B %e') # e.g. 'June 13'
        # from http://docs.python.org/library/datetime.html#strftime-strptime-behavior
	# no %e, use %d instead of %e. ian#2012-6-5 
        out = then.strftime('%B %d') # e.g. 'June 13'
 Posted by ian at 06:53  Tagged with: OpenSourcePythonweb.py
UserWarning: Could not infer format, so each element will be parsed individually, falling back to `dateutil`. To ensure parsing is consistent and as-expected, please specify a format. data['日期'] = pd.to_datetime(data['年'].astype(str) + '-' + data['月'].astype(str) + '-' + data['日'].astype(str)) Traceback (most recent call last): File "D:\Onedrive\Desktop\人工智能应用\TEST.py", line 4, in <module> data['日期'] = pd.to_datetime(data['年'].astype(str) + '-' + data['月'].astype(str) + '-' + data['日'].astype(str)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\pandas\core\tools\datetimes.py", line 1067, in to_datetime values = convert_listlike(arg._values, format) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\pandas\core\tools\datetimes.py", line 435, in _convert_listlike_datetimes result, tz_parsed = objects_to_datetime64( ^^^^^^^^^^^^^^^^^^^^^^ File "D:\python\Lib\site-packages\pandas\core\arrays\datetimes.py", line 2398, in objects_to_datetime64 result, tz_parsed = tslib.array_to_datetime( ^^^^^^^^^^^^^^^^^^^^^^^^ File "tslib.pyx", line 414, in pandas._libs.tslib.array_to_datetime File "tslib.pyx", line 596, in pandas._libs.tslib.array_to_datetime File "tslib.pyx", line 553, in pandas._libs.tslib.array_to_datetime File "conversion.pyx", line 641, in pandas._libs.tslibs.conversion.convert_str_to_tsobject File "parsing.pyx", line 336, in pandas._libs.tslibs.parsing.parse_datetime_string File "parsing.pyx", line 666, in pandas._libs.tslibs.parsing.dateutil_parse pandas._libs.tslibs.parsing.DateParseError: Unknown datetime string format, unable to parse: 2016.0-6.0-1, at position 0
05-24
music-backend/ ├── .env # 环境变量配置 ├── .flaskenv # Flask环境配置 ├── requirements.txt # Python依赖包 ├── run.py # 主启动脚本 ├── tasks.py # 后台任务脚本 │ ├── migrations/ # 数据库迁移目录(自动生成) │ ├── versions/ │ └── alembic.ini │ ├── app/ # 主应用目录 │ ├── __init__.py # 应用工厂 │ ├── config.py # 配置管理 │ ├── models.py # 数据库模型 │ │ │ ├── routes/ # API路由 │ │ ├── __init__.py │ │ ├── auth.py # 认证路由 │ │ ├── music.py # 音乐管理 │ │ ├── playlist.py # 歌单管理 │ │ └── user.py # 用户管理 │ │ │ ├── services/ # 业务服务 │ │ ├── __init__.py │ │ ├── audio_processor.py # 音频处理 │ │ ├── database.py # 数据库服务 │ │ └── recommendation.py # 推荐服务(如有) │ │ │ ├── static/ # 静态资源 │ │ └── uploads/ # 上传文件目录 │ │ │ ├── templates/ # HTML模板(可选) │ │ └── email/ # 邮件模板 │ │ │ ├── utils/ # 工具类 │ │ ├── __init__.py │ │ ├── file_utils.py # 文件处理 │ │ └── auth_utils.py # 认证工具 │ │ │ └── errors/ # 错误处理 │ ├── handlers.py # 错误处理器 │ └── error_codes.py # 错误码定义 │ └── tests/ # 测试目录 ├── unit/ │ ├── test_models.py │ ├── test_routes.py │ └── test_services.py └── integration/ └── test_api.py 这是我的后端项目结构,根据上面的内容将其补充完整
10-15
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值