今天遇到一个报错: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. Java代码优化,都有哪些常用方法?

    Java代码优化是Java编程开发很重要的一个步骤,Java代码优化要注重细节优化,一个两个的细节的优化,产生的效果不大,但是如果处处都能注意代码优化,对代码减少体积.提高代码运行效率是有巨大帮助的, ...

  2. linux c/c++ 文件是否存在

    linux c/c++ 文件是否存在 #include <unistd.h> int FileExist(const char* fname) { return access(fname, ...

  3. 学习笔记10—Python 绘图集

    ordered_data = np.load('ordered_data_just_TD_mae.npy')results = pd.Series(np.squeeze(np.load('result ...

  4. Spring的AOP配置文件和注解实例解析

    1.1           Spring的AOP配置文件和注解实例解析 AOP它利用一种称为"横切"的技术,将那些与核心业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减 ...

  5. (转)c# 断言类

    Assert 类 使用 true/false 命题验证单元测试中的条件. 继承层次结构 System.Object Microsoft.VisualStudio.TestTools.UnitTesti ...

  6. boke例子:用户登录

    boke例子:用户登录 1.首先创建user表,authority表(角色),user_authority,表(用户角色表) Authority实体类,需要继承:GrantedAuthority类, ...

  7. 雷林鹏分享:jQuery EasyUI 表单 - 格式化下拉框

    jQuery EasyUI 表单 - 格式化下拉框 本教程向您展示如何创建一个简单的下拉框(Combobox),让它在下拉框中显示图片项.您可以在下拉框(combobox)上使用 formatter ...

  8. English trip V1 - B 16. Giving Reasons 提供个人信息 Teacher:Lamb Key: Why/Because

    In this lesson you will learn how to give reasons for something you've done. 课上内容(Lesson) Why do peo ...

  9. 20170821xlVBA跨表公式套用

    Public Sub CopyModelHideBlankRows() AppSettings Dim StartTime As Variant Dim UsedTime As Variant Sta ...

  10. You Don't Know JS: this & Object Prototypes (第6章 Behavior Delegation)附加的ES6 class未读

    本章深挖原型机制. [[Prototype]]比类更直接和简单! https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%2 ...