今天遇到一个报错: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. leecode第六十一题(旋转链表)

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  2. 用 JavaScript 将网站后台的数据变化实时更新到前端

    1.ajax短连接:客户端每隔一秒钟发一次请求,服务器收到请求后会立刻返回结果,不管有没有新数据.2.ajax长连接:客户端发送一次请求,服务器端收到请求后查询有没有新数据,如果没有新数据就阻塞这个请 ...

  3. Codeforces 233 D - Table

    D - Table 思路:dp 首先,第i列的个数肯定和第i - n列个数一样,假设[i - n + 1, i - 1] 之间的个数之和为x,那么第i列和第i-n列的个数应该是n - x 那么我们可以 ...

  4. 解决ios下的微信页面背景音乐无法自动播放问题

    在做各种html5场景页面的时候,插入背景音乐是一个很普遍的需求,我们都知道,ios下的safari是无法自动播放音乐的,以至于现在行程一种认知,ios是没有办法自动播放媒体资源的,这个认知其实是错误 ...

  5. Unity --- 纹理为什么要设置为2的N次方

    1.图片的纹理像素在Unity3D中需要遵循2的N次方,由图形学决定的,只识别2的N次方.   非2的N次方的图片会转化为2的N次方图片(500 x 500 → 512 x 512),是因为转化过程比 ...

  6. stm32cube使用

    1.使用stm32cube生成CAN代码注意事项: a.需要手动配置CAN过滤器 { CAN_FilterConfTypeDef sFilterConfig; uint32_t filterID = ...

  7. English trip V1 - 10.Family Ties 家庭关系 Teacher:Emily Key: Possessive s (所有格 s)

    In this lesson you will learn to talk about people in a family. 课上内容(Lesson) What are you Spring Fes ...

  8. 20170914xlVBA通讯公司分类汇总

    Sub 租房() Dim Wb As Workbook Dim Sht As Worksheet Dim OpenWb As Workbook Dim OpenSht As Worksheet Dim ...

  9. python记录_day10 动态传参 命名空间 作用域

    一.动态传参 动态传参用到 *args 和 **kwargs ,*号表示接收位置参数,args是参数名:**表示接收关键字参数,kwargs是参数名 def chi(*food): print(foo ...

  10. Android测试(二)——drozer使用

    drozer启动: 1)首先在模拟 器或者安卓设备上开启drozer; 2)然后打开adb,转发端口: adb forward tcp:31415 tcp:31415 3)在电脑上开启drozer: ...