今天遇到一个报错: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. macOS下Hive 2.x的安装与配置

    1 简介 Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的[SQL]查询功能,可以将SQL语句转换为MapReduce任务进行运行.其优点是学习成本 ...

  2. leecode第五十四题(螺旋矩阵)

    class Solution { public: vector<int> spiralOrder(vector<vector<int>>& matrix) ...

  3. Flask请求上下文request

  4. redhat 7.0 配置Bond

    把/etc/sysconfig/network-scripts/目录下的ifcfg-bond*文件手动移动到/tmp目录. 重启网络:systemctl restart NetworkManager ...

  5. 子序列的按位或 Bitwise ORs of Subarrays

    2018-09-23 19:05:20 问题描述: 问题求解: 显然的是暴力的遍历所有的区间是不可取的,因为这样的时间复杂度为n^2级别的,对于规模在50000左右的输入会TLE. 然而,最后的解答也 ...

  6. centos 安装npm node

    最近那vue全套造了个管理系统的轮子,发现node简直太好用了. elment-UI的出现就是不懂ui设计的后台工程师的福音~ 正好自己买的两个云服务器空闲着没用,就拿来试试看了 首先软件都安装在/u ...

  7. slf4j/logback: logging日志的配置

    slf4j/logback: logging日志的配置 import依赖: import org.slf4j.Logger;import org.slf4j.LoggerFactory;private ...

  8. LeetCode--255--用队列实现栈(java版)

    使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...

  9. Rest_framework 和路由配置(一)

    简介 Django REST framework是一个建立在Django基础之上的Web 应用开发框架,可以快速的开发REST API接口应用. Rest_framework 核心思想: 缩减代码. ...

  10. HTML 弹出遮罩层一(遮罩层和内容标签嵌套)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...