windows7,python3使用time.strftime()函数报ValueError: embedded null byte

windows7,python3使用time.strftime()函数报ValueError: embedded null byte
embedded null byte

windows7环境下,执行代码报ValueError: embedded null byte时,在原代码前面加一行代码:locale.setlocale(locale.LC_ALL,’en’)即可解决,即:

locale.setlocale(locale.LC_ALL,'en') 
locale.setlocale(locale.LC_CTYPE,'chinese')
ws["C3"]=time.strftime('%Y年%m月%d日 %H时%M分%S秒',time.localtime())

转载补充说明

Win10环境、Python3.8也遇到该问题,其中设置locale的时候需要导入这个包,具体执行代码如下:

>>> import time
>>> time.strftime('%Y年%m月%d日 %H时%M分%S秒',time.localtime())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: embedded null byte
>>> locale.setlocale(locale.LC_ALL,'en')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'locale' is not defined
>>> import locale
>>> locale.setlocale(locale.LC_ALL,'en')
'en'
>>> locale.setlocale(locale.LC_CTYPE,'chinese')
'Chinese_China.936'
>>> time.strftime('%Y年%m月%d日 %H时%M分%S秒',time.localtime())
'2022年12月16日 21时32分02秒'

本文从博客园(点击查看原文)转载而来。不代表烟海拾贝立场,如若转载,请注明出处:https://somirror.com/3343.html

(0)
上一篇 2022-12-16 21:08
下一篇 2022-12-17 15:34

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注