今天遇到一个报错: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的更多相关文章

  1. 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: ...

  2. dbfread报错ValueError错误解决方法

    问题 我在用dbfread处理.dbf数据的时候出现了报错 ValueError("could not convert string to float: b'.'",) 然后查找. ...

  3. 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 ...

  4. matplotlib.pyplot import报错: ValueError: _getfullpathname: embedded null character in path

    Environment: Windows 10, Anaconda 3.6 matplotlib 2.0 import matplotlib.pyplot 报错: ValueError: _getfu ...

  5. 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 ...

  6. moviepy音视频剪辑VideoClip类fl_image方法image_func报错ValueError: assignment destination is read-only解决办法

    ☞ ░ 前往老猿Python博文目录 ░ moviepy音视频剪辑模块的视频剪辑基类VideoClip的fl_image方法用于进行对剪辑帧数据进行变换. 调用语法:fl_image(self, im ...

  7. tensorflow-TFRecord报错ValueError: Protocol message Feature has no "feature" field.

    编写代码用TFRecord数据结构存储数据集信息是报错:ValueError: Protocol message Feature has no "feature" field.或和 ...

  8. Nullable<System.DateTime>日期格式转换 (转载)

    一.问题 1.html页面中时间显示出错,数据库中时间是正确的. 原因:没有把DateTime转成String类型. 2.  在C#中,发现不能直接使用ToString("yyyy-MM-d ...

  9. 标记编码报错ValueError: bad input shape ()

    <Python机器学习经典实例>2.9小节中,想自己动手实践汽车特征评估质量,所以需要对数据进行预处理,其中代码有把字符串标记编码为对应的数字,如下代码 input_data = ['vh ...

随机推荐

  1. 写了一个Hy的vscode语法高亮插件

    -------2018 8 3----------- 把函数名和参数改了,正则有点古怪,参考自带的lambda表达式才搞定 但彩色括号走了弯路,各种配图有彩色括号的插件其实很少是自己搞的,其实只要再装 ...

  2. spring aop通过注解实现日志记录

    首先是几个概念:连接点(Joinpoint).切点(Pointcut).增强(Advice).切面(Aspect) 另外也要使用到注解. 需求:通过注解定义LogEnable.然后程序运行能够识别定义 ...

  3. 酷开 5.5 版本安装第三方app

    https://www.znds.com/jc/article/2952-1.html .开始安装(以安装当贝桌面为例): adb connect 192.168.XXX.XXX(电视IP) adb ...

  4. 7内存管理-MRC

    @0简介 内存管理,即内存里各个对象的管理,即内存里各个对象的生命周期的管理,(从面向对象的角度看) @1引用计数器 默认为1,即有一滴的生命血液,若为0就会死去 @2单个对象的管理 自己管理自己,自 ...

  5. jmap -histo pid 输出的[C [B [I [S 的含义

    JMAP 输出 其中: [C is a char[][S is a short[][I is a int[][B is a byte[][[I is a int[][]

  6. spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略

    spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略 某电子商务网站在一个黑色星期五 ...

  7. U8800 手机恢复出厂设置出现轻触Android开始页面 处理办法

    U8800 恢复出厂设置出现轻触Android开始页面处理办法 问题现象 Huawei 手机U8800刷机后,点击恢复出厂默认设置后,出现了一个一直停留在“欢迎使用Android 轻触 Android ...

  8. Tomcat ----> 学习笔记

    源码之几个常见类和接口的关系 在学习Servlet的时候经常见到以下几个合成单词和非合成单词:Servlet.GenericServlet.HttpServlet.它们之间有联系的.接下来我把它们的联 ...

  9. OSPF - 1,基础

    1,OSPF知识点a)在OSPF中,如果是环回口宣告进OSPF,不管宣告时配置的是多少位掩码,路由器收到的都是32位.(EIGRP配了多少位就收到多少位).好处:EIGRP中,在PING包发起时如果在 ...

  10. 8.2 DRAM和SRAM

    计算机组成 8 存储层次结构 8.2 DRAM和SRAM SRAM比较快,DRAM比较慢:SRAM比较贵,DRAM比较便宜.记住这些结论是很容易的,但是比是什么更重要的是为什么.那在这一节我们就从电路 ...