关于TypeError: strptime() argument 1 must be str, not bytes解析
关于TypeError: strptime() argument 1 must be str, not bytes解析
在使用datetime.strptime(s,fmt)来输出结果日期结果时,出现错误
TypeError: strptime() argument 1 must be str, not bytes
我的源代码如下
def datestr2num(s):
return datetime.strptime(s, "%d-%m-%Y").date().weekday()
dates=np.loadtxt('data.csv', delimiter=',', usecols=(1,), converters={1: datestr2num}, unpack=True)
data.csv内容如下

编译器在打开data.csv文件时,将表格里的第2列数组值提取出来返回给dates,第二列值是日期格式字符串,但因为我们是以二进制编码的格式打开第二列值是,返回的值字节字符串bytes,所以需要把它便会string,则对字符串解码用函数decode('asii'),变成string格式。
def datestr2num(s):
return datetime.strptime(s.decode('ascii'), "%d-%m-%Y").date().weekday()
dates=np.loadtxt('data.csv', delimiter=',', usecols=(1,), converters={1: datestr2num}, unpack=True)
从网上摘抄的英文解释如下:
line is a bytestring, because you opened the file in binary mode. You'll need to decode the string; if it is a date string matching the pattern, you can simply use ASCII:
time.strptime(line.decode('ascii'), '%Y-%m-%d ...')
You can add a 'ignore' argument to ignore anything non-ASCII, but chances are the line won't fit your date format then anyway.
Note that you cannot pass a value that contains more than the parsed format in it; a line with other text on it notexplicitly covered by the strptime() pattern will not work, whatever codec you used.
And if your input really varies that widely in codecs, you'll need to catch exceptions one way or another anyway.
Aside from UTF-16 or UTF-32, I would not expect you to encounter any codecs that use different bytes for the arabic numerals. If your input really mixes multi-byte and single-byte codecs in one file, you have a bigger problem on your hand, not in the least because newline handling will be majorly messed up.
关于TypeError: strptime() argument 1 must be str, not bytes解析的更多相关文章
- Python之scrapy框架之post传输数据错误:TypeError: to_bytes must receive a unicode, str or bytes object, got int
错误名:TypeError: to_bytes must receive a unicode, str or bytes object, got int 错误翻译:类型错误:to_bytes必须接收u ...
- HTTPResponse object — JSON object must be str, not 'bytes'
http://stackoverflow.com/questions/24069197/httpresponse-object-json-object-must-be-str-not-bytes HT ...
- Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+
今天写上传文件代码,如下 def uploadHandle(request): pic1=request.FILES['pic1'] picName=os.path.join(settings.MED ...
- Python - TypeError: unicode argument expected, got 'str'
参考:TypeError: unicode argument expected, got 'str' Python代码: from io import StringIO def main(): f = ...
- Python错误TypeError: write() argument must be str, not bytes
2016-07-03 20:51:25 今天使用Python中的pickle存储的时候出现了以下错误: TypeError: write() argument must be str, not byt ...
- TypeError: write() argument must be str, not bytes报错
TypeError: write() argument must be str, not bytes 之前文件打开的语句是: with open('C:/result.pk','w') as fp: ...
- TypeError: write() argument must be str, not bytes报错原因及Python3写入二进制文件方法
Python2随机写入二进制文件: with open('/python2/random.bin','w') as f: f.write(os.urandom(10)) 但使用Python3会报错: ...
- python write() argument must be str, not bytes
python pickle from __future__ import absolute_import from __future__ import division from __future__ ...
- Python报错TypeError: '<' not supported between instances of 'str' and 'int'
n = input() if n>=100:print(int(n)/10) else:print(int(n)*10) 报错内容: Traceback (most recent call la ...
随机推荐
- Mysql 创建用户授权
MySQL创建用户与授权 一. 创建用户 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明: username:你将创建的用 ...
- 深度原理与框架-图像超分辨重构-tensorlayer
图像超分辨重构的原理,输入一张像素点少,像素较低的图像, 输出一张像素点多,像素较高的图像 而在作者的文章中,作者使用downsample_up, 使用imresize(img, []) 将图像的像素 ...
- Python module ---- os
os 模块Python标准库中的一个用于访问操作系统功能的模块,使用OS模块中提供的接口,可以实现跨平台访问.提供了一个统一的操作系统接口函数, 这些接口函数通常是平台指定的,os 模块能在不同操作系 ...
- GDI+_从Bitmap里得到的Color数组值解决方案
' InkHin_ZhiZhuo ' Date :2019.2.18 ' E-mail lqx@tyningling.Top 'This function and Module is written ...
- 我的vimrc设置
vim一个文件 :e version :editor version 查看.vimrc所在的系统和用户文件 vim ~/.vimrc " 行号 set number " 语法高亮( ...
- React Native: unable to load scripts from assets 'index.android.bundle' on real device
问题:重新建了一个项目后,运行react-native run-android报: unable to load scripts from assets 'index.android.bundle' ...
- 同机器与不同机器redis集群
此文为另外一位大神原创由于没有找到分享功能 粘贴复制到此 原地址为https://blog.csdn.net/u012042021/article/details/72818759 一.同机器下的 ...
- js解决下拉列表框互斥选项的问题
如图不区分选项与其他选项是互斥的关系,当选择了不区分时,其他选项就要去除,当有其他选项存在时,就不能有不区分 解决办法:定义change事件,若列表发生改变,首先判断点击的是否是不区分,若是,则将其他 ...
- linux下面重启nfs报错:nfs-server.service:main process exited
linux下面重启nfs报错:nfs-server.service:main process exited [root@dhcp-66-83-39 images]# service rpcbind s ...
- unique_ptr
[unique_ptr] unique_ptr 不共享它的指针.它无法复制到其他 unique_ptr,无法通过值传递到函数,也无法用于需要副本的任何标准模板库 (STL) 算法.只能移动unique ...