time.strftime:格式化字符串中含中文报错处理

import time
print(time.strftime("%Y年%m月%d日 %H时%M分%S秒",time.localtime()))

报错:

UnicodeEncodeError: ‘locale‘ codec can‘t encode character ‘\u5e74‘ in position 2: Illegal byte sequence

打印当前时间

import time
print(time.localtime())

运行结果:

time.struct_time(tm_year=2019, tm_mon=1, tm_mday=30, tm_hour=11, tm_min=22, tm_sec=48, tm_wday=2, tm_yday=30, tm_isdst=0)

格式化输出

import time
print(time.strftime("%Y{}%m{}%d{} %H{}%M{}%S{}",time.localtime()).format(‘年‘,‘月‘,‘日‘,‘时‘,‘分‘,‘秒‘))

运行结果:

2019年01月30日 11时22分48秒

为什么不能直接写成字符串,没有理解。

原文地址:https://www.cnblogs.com/raffia/p/10337348.html

时间: 2024-08-27 15:19:18

time.strftime:格式化字符串中含中文报错处理的相关文章

解决在读取properties文件中出现中文报错问题

// 读取properties中的参数 Properties properties = new Properties(); // 读取properties文件 使用InputStreamReader来解决中文报错问题 InputStreamReader inputStreamReader = null; // InputStream inputStream = Main.class.getResourceAsStream("/file.properties"); // 需要遍历的路径

mybatis中xml的sql之test中文报错

在mybatis中sql,test中文报错( java.lang.NumberFormatException 这句话明确告诉了我们是数字格式异常).需加.tostring(). <if test="bookName == '毛选集'.tostring() "> and b.book_Name like #{bookName} </if>

string.format格式化字符串中转义大括号“{}”

用C#写程序操作Excel,读取单元格内容根据所需格式生成字符串,使用String.Format(string format,object arg0)方法.以前只知"{0}"为索引占位符(即格式项),与参数列表中的第一个对象相对应,格式设置过程将每个格式项替换为对应对象的值的文本表示形式.但这次需将参数对象格式成一对大括号括起来的格式,即返回字符串"{对象arg0的文本表示形式}". //直接加大括号 String.Format("{{0}}",

C#格式化字符串中转义大括号“{}”

原文:C#格式化字符串中转义大括号"{}" 今天,用C#写程序操作Excel,读取单元格内容根据所需格式生成字符串,使用String.Format(string format,object arg0)方法.以前只知"{0}"为索引占位符(即格式项),与参数列表中的第一个对象相对应,格式设置过程将每个格式项替换为对应对象的值的文本表示形式.但这次需将参数对象格式成一对大括号括起来的格式,即返回字符串"{对象arg0的文本表示形式}". [cshar

网上找的JS截取字符串(含中文)

<script> /* 2007-11-28 XuJian */ //截取字符串 包含中文处理 //(串,长度,增加...) function subString(str, len, hasDot) { var newLength = 0; var newStr = ""; var chineseRegex = /[^\x00-\xff]/g; var singleChar = ""; var strLength = str.replace(chines

C# 删除字符串中的中文

1 /// <summary> 2 /// 删除字符串中的中文 3 /// </summary> 4 public static string Delete中文(string str) 5 { 6 string retValue = str; 7 if (System.Text.RegularExpressions.Regex.IsMatch(str, @"[\u4e00-\u9fa5]")) 8 { 9 retValue = string.Empty; 10

传入一个中文字符串,返回一个字符串中的中文拼音

/** * @param 传入一个中文字符串 * @return 返回一个字符串中的中文拼音 */ private String getNameNum(String name) { if (!Utils.isStrEmpty(name)) { int len = name.length(); char[] nums = new char[len]; for (int i = 0; i < len; i++) { String tmp = name.substring(i); nums[i] =

Pycharm中不支持中文编码的解决方案。Pycharm中文报错。 Pycharm出现的部分快捷键无效及解决办法

Pycharm中不支持中文编码的解决方案.Pycharm中文报错. 1. 打开Pycharm ---->  File ----> Default setting ------> Editor -------> File Encodings ,如下图,设置成UTF-8, 然后应用 2.  点击[File]---[Setting]---[Editor]---[File and Code Templates],点击右边的[python script],在编辑框中输入: #-*-codin

客户端cmd打开mysql,执行插入中文报错或插入中文乱码解决方案

最近在制作一个安装包,需要安装的时候执行mysql脚本儿,做了一个批处理,但是发现总是执行到 插入中文的时候报错,或者插入中文是乱码. 网上查了好多资料,说是把编码改成GBK什么的,终究还是不成功. 最后经过多次测试,现把解决方案分享给大家. 第一步:打开mysql中的配置文件,my.ini,看一看配置文件中 [mysql] default-character-set=utf8 [mysqld] character-set-server=utf8 看是不是这样配置的.因为utf8 是国际通用的,