使用time.strftime将 "2020-10-10 10:10:10" 转化为  2020年10月10日10时10分10 报错:

import time
timestr="2020-10-10 10:10:10"
t=time.strptime(timestr,"%Y-%m-%d %H:%M:%S")
print(time.strftime("%Y年%m月%d日 %H时%M分%S秒",t))

根据错误可以看出,没有执行成功的原因是"%Y年%m月%d日 %H时%M分%S秒"中包含了中文,中文没有转化为unicode编码失败的。

解决方法:

方法一:先转为uncode编码执行,执行完后转为utf-8显示

import time
timestr="2020-10-10 10:10:10"
t=time.strptime(timestr,"%Y-%m-%d %H:%M:%S")
print(time.strftime("%Y年%m月%d日 %H时%M分%S秒".encode('unicode_escape').decode('utf8'),t).encode('utf-8').decode('unicode_escape'))

执行结果:

  

方法二:修改语言符号 详情

import time,locale
timestr="2020-10-10 10:10:10"
t=time.strptime(timestr,"%Y-%m-%d %H:%M:%S")
locale.setlocale(locale.LC_CTYPE,'chinese')
print(time.strftime("%Y年%m月%d日 %H时%M分%S秒",t))

执行结果:

  

方法三:重写一个自定义转化函数

def change_time(timeStr:str,t_int=False)->str:
import re
t_text = ['年', '月', '日 ', '时', '分', '秒']
re_t = re.compile("[\d|\.]+")
str_time = ''
for k, v in zip(t_text, re_t.findall(timeStr)):
if t_int and '.' in v :
v=re.sub('\.\d+', '', v)
str_time += str(v) + k
return str_time if __name__ == '__main__':
print(datetime.now())
t=change_time(str(datetime.now()))
int_t=change_time(t,True)
float_t=change_time(t)
print(int_t)
print(float_t)

执行结果:

python中time.strftime不支持中文,报错UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: encoding error的更多相关文章

  1. python 格式化时间含中文报错: 'locale' codec can't encode character '\u5e74'

    执行下面代码报错: UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal ...

  2. 接口测试中读取excel中的请求数据含有中文问题,UnicodeEncodeError: 'latin-1' codec can't encode character '\u5c0f' in position

    错误信息:UnicodeEncodeError: 'latin-1' codec can't encode character '\u5c0f' in position 31: Body ('小') ...

  3. 关于编码问题,报错:'gbk' codec can't encode character '\u3164' in position 0: illegal multibyte sequence

    之前经常在写入文件的时候遇到这种报错, 'gbk' codec can't encode character '\u3164' in position 0: illegal multibyte seq ...

  4. python基础===解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX(转载)

    本文转自:解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX 从网上抓了一些字 ...

  5. 报错处理(UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 2: ill egal multibyte sequence)

    参照文[https://blog.csdn.net/Dillon2015/article/details/53204955]的说法, 第一个错 [UnicodeEncodeError:'gbk' co ...

  6. 爬取网页内容后写入文件报错UnicodeEncodeError: 'gbk' codec can't encode的问题解决方案

    老猿使用如下代码读取网页内容: req = urllib.request.Request(url=url,headers=header) text = urllib.request.urlopen(r ...

  7. python 写入日志的问题 UnicodeEncodeError: 'gbk' codec can't encode character '\xbb' in position 0: illegal multibyte sequence

    最近,使用python的logging模块,因为这个写入日志写完后就没有管它.在存储日志信息的时候,一直提示: UnicodeEncodeError: 'gbk' codec can't encode ...

  8. python编码问题——解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX

    python实现爬虫遇到编码问题: error:UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX ...

  9. python+selenium运行报错UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

    使用python+selenium运行自动化脚本时,打印某一段文字出现UnicodeEncodeError: 'ascii' codec can't encode characters in posi ...

随机推荐

  1. #华为云·寻找黑马程序员#【代码重构之路】如何“消除”if/else

    1. 背景 if/else是高级编程语言中最基础的功能,虽然 if/else 是必须的,但滥用 if/else,特别是各种大量的if/else嵌套,会对代码的可读性.可维护性造成很大伤害,对于阅读代码 ...

  2. 【nodejs原理&源码赏析(4)】深度剖析cluster模块源码与node.js多进程(上)

    [摘要] 集群管理模块cluster浅析 示例代码托管在:http://www.github.com/dashnowords/blogs 一. 概述 cluster模块是node.js中用于实现和管理 ...

  3. 深入比特币原理(三)——交易的输入(input)与输出(output)

    本节内容非常重要,如果你不能很好的掌握本节内容,你无法真正理解比特币的运行原理,请务必要学习清楚. 比特币的交易模型为UTXO(unspend transaction output),即只记录未花费的 ...

  4. [Mathematics][BJTU][Calculus]Detailed explanations and proofs of the Dirac-Abel Discriminant Methods which deal with the conditional convergence

    So, today we will talk about the conditional convergence and two discriminant methods, namely Dirac- ...

  5. Android多图选择

    多图选择是Android中一个常用的功能,用户可以拍照或者批量选择图片上传,还是国际惯例,先看下效果图,demo地址我会放到文章末尾. 经过对比,这里我选择了一个第三方开源库PictureSelect ...

  6. 2018HDU多校训练-3-Problem F. Grab The Tree

    Little Q and Little T are playing a game on a tree. There are n vertices on the tree, labeled by 1,2 ...

  7. 2018HDU多校训练一 D Distinct Values

    hiaki has an array of nn positive integers. You are told some facts about the array: for every two e ...

  8. CodeForces1006A - Adjacent Replacements

    A. Adjacent Replacements time limit per test 1 second memory limit per test 256 megabytes input stan ...

  9. C++ 并发编程指南(收藏笔记)

    git地址: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice https://github.com/forhappy/Cpl ...

  10. FPGA_VIP_V101 视频开发板 深入调试小结

    FPGA_VIP_V101 推出已经有半年有余,各项功能例程已移植完毕,主要参考crazybingo例程进行移植和结合开发板设计了几个实例例程 主要包含: 硬件配置: FPGA:EP4CE6E22C8 ...