datetime.strptime格式转换报错ValueError
今天遇到一个报错:ValueError: time data '2018-10-10(Wednesday) AM0:50' does not match format '%Y-%m-%d(%A) %p%I:%M'。
代码追踪到如下几行:
class Chatsonline:
def __init__(self,filename, sheet_name,template_id):
self.filename = filename
self.sheet_name = sheet_name
self.template_id = template_id
self.conn = pymysql.connect(host=host, user=user,
password=password, port=port, charset=charset, db=db)
self.cur = self.conn.cursor() def format_datetime(self,string):
date_str = string.replace(u"年", "-").replace(u"月", "-").replace(u"日", "").replace(u"星期一", "Monday").replace(u"星期二", "Tuesday").replace(u"星期三", "Wednesday").replace(u"星期四", "Thursday").replace(u"星期五", "Friday").replace(u"星期六", "Saturday").replace(u"星期天", "Sunday")
import re
if u"中午" in string:
hours = int(re.split(u"[中午:]",date_str)[-2])
if hours >=12:
date_str = date_str.replace(u"中午", "PM")
else:
date_str = date_str.replace(u"中午", "AM")
else:
'''20181016约定没有凌晨只分上下午,凌晨0点应该对应上午12点,12小时制为[AM12,AM1,AM2,...,AM11,PM12,PM1,PM2,...,PM23]'''
date_str = date_str.replace(u"上午", "AM").replace(u"下午", "PM").replace(u"凌晨", "AM").replace(u"晚上", "PM")
return datetime.strptime(date_str, "%Y-%m-%d(%A) %p%I:%M")
转换时间格式:"%Y-%m-%d(%A) %p%I:%M"
%Y:年 %m:月 %d:日 %A:星期全名 %p:AM/PM %I:12小时制 %M:分钟
在文件上传的时候,时间列原始数据为:2018年10月10日(星期三) 凌晨0:50,而在十二小时制中,表示凌晨0点应该为AM12,所以约定以后上传文件时间只有上午下午之分,
12小时制为[AM12,AM1,AM2,...,AM11,PM12,PM1,PM2,...,PM23],对应24小时制[0点,1点,2点,...,11点,12点,1点,2点,...,23点]。
datetime.strptime格式转换报错ValueError的更多相关文章
- tensorflow读取jpg格式图片报错 ValueError: Only know how to handle extensions: ['png']; with Pillow installed matplotlib can handle more images
当运行mpimg.imread("img.jpg")时,spyder 出现如下错误: ValueError: Only know how to handle extensions: ...
- dbfread报错ValueError错误解决方法
问题 我在用dbfread处理.dbf数据的时候出现了报错 ValueError("could not convert string to float: b'.'",) 然后查找. ...
- keras 报错 ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("embedding_1/random_uniform:0", shape=(5001, 128), dtype=float32)'
在服务器上训练并保存模型,复制到本地之后load_model()报错: ValueError: Tensor conversion requested dtype int32 for Tensor w ...
- matplotlib.pyplot import报错: ValueError: _getfullpathname: embedded null character in path
Environment: Windows 10, Anaconda 3.6 matplotlib 2.0 import matplotlib.pyplot 报错: ValueError: _getfu ...
- C# MVC 用户登录状态判断 【C#】list 去重(转载) js 日期格式转换(转载) C#日期转换(转载) Nullable<System.DateTime>日期格式转换 (转载) Asp.Net MVC中Action跳转(转载)
C# MVC 用户登录状态判断 来源:https://www.cnblogs.com/cherryzhou/p/4978342.html 在Filters文件夹下添加一个类Authenticati ...
- moviepy音视频剪辑VideoClip类fl_image方法image_func报错ValueError: assignment destination is read-only解决办法
☞ ░ 前往老猿Python博文目录 ░ moviepy音视频剪辑模块的视频剪辑基类VideoClip的fl_image方法用于进行对剪辑帧数据进行变换. 调用语法:fl_image(self, im ...
- tensorflow-TFRecord报错ValueError: Protocol message Feature has no "feature" field.
编写代码用TFRecord数据结构存储数据集信息是报错:ValueError: Protocol message Feature has no "feature" field.或和 ...
- Nullable<System.DateTime>日期格式转换 (转载)
一.问题 1.html页面中时间显示出错,数据库中时间是正确的. 原因:没有把DateTime转成String类型. 2. 在C#中,发现不能直接使用ToString("yyyy-MM-d ...
- 标记编码报错ValueError: bad input shape ()
<Python机器学习经典实例>2.9小节中,想自己动手实践汽车特征评估质量,所以需要对数据进行预处理,其中代码有把字符串标记编码为对应的数字,如下代码 input_data = ['vh ...
随机推荐
- JS _proto_ 和 prototype
转载自:https://www.cnblogs.com/wuwenjie/p/5433776.html 大佬讲得很明白,自己也收藏一下! 初学javascript的时候也跟题主一样搞不清楚,自己好好总 ...
- python三级菜单实例(傻瓜版和进阶版)
程序: python三级菜单 要求: : 1.打印省.市.县三级菜单 2.可返回上一级 3.可随时退出程序 方案一:傻瓜版(其实傻瓜版考察的主要是思路!思路清楚了,那才不是傻瓜!O(∩_∩)O哈哈~) ...
- Polygenic score
We estimate the maximum prediction accuracy for the risk of Alzheimer's disease based on disease pre ...
- tornado web
tornado web frame: 非阻塞服务器,速度快,运用epoll 模板语言+render(),实现根据用户输入,自动渲染页面的动态效果. 在使用模板前需要在setting中设置模板路径: s ...
- Tensorflow手写数字识别---MNIST
MNIST数据集:包含数字0-9的灰度图, 图片size为28x28.训练样本:55000,测试样本:10000,验证集:5000
- jq 倒计时
引入jq 注 在IE和safari 如果时间出现NAN 将时间格式改为YYYY/MM/DD HH:MM:SS 例:var d2 = new Date("2019/02/18 15:59&qu ...
- python time 表示方式
- blog项目知识点梳理
1.获取图片验证码: def get_validCode_img(request): # 方式1: # import os # path= os.path.join(settings.BASE_DIR ...
- leetcode-algorithms-26 Remove Duplicates from Sorted Array
leetcode-algorithms-26 Remove Duplicates from Sorted Array Given a sorted array nums, remove the dup ...
- servlet初认知(持续更新中)
一:前言: 一个Servlet程序其实就是一个实现了Java特殊接口的类,它由支持Servlet(具有Servlet引擎)的WEB服务器调用和启动运行.一个Servlet程序负责处理它对应的一个或者多 ...