关于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.

 
 
posted @ 2017-09-08 20:48 樟樟22 阅读(...) 评论(...) 编辑 收藏
 
以上为转载!!

关于TypeError: strptime() argument 1 must be str, not bytes解析的更多相关文章

  1. 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 ...

  2. HTTPResponse object — JSON object must be str, not 'bytes'

    http://stackoverflow.com/questions/24069197/httpresponse-object-json-object-must-be-str-not-bytes HT ...

  3. Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+

    今天写上传文件代码,如下 def uploadHandle(request): pic1=request.FILES['pic1'] picName=os.path.join(settings.MED ...

  4. Python - TypeError: unicode argument expected, got 'str'

    参考:TypeError: unicode argument expected, got 'str' Python代码: from io import StringIO def main(): f = ...

  5. 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 ...

  6. TypeError: write() argument must be str, not bytes报错

    TypeError: write() argument must be str, not bytes 之前文件打开的语句是: with open('C:/result.pk','w') as fp: ...

  7. TypeError: write() argument must be str, not bytes报错原因及Python3写入二进制文件方法

    Python2随机写入二进制文件: with open('/python2/random.bin','w') as f: f.write(os.urandom(10)) 但使用Python3会报错: ...

  8. python write() argument must be str, not bytes

    python pickle from __future__ import absolute_import from __future__ import division from __future__ ...

  9. 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 ...

随机推荐

  1. linux虚拟机长时间不用时间改回来的方法

    1. 时间和时区 地理课上我们都学过格林威治时间(GMT), 它也就是0时区时间,但是我们在计算机中经常看到的是UTC,它是Coordinated Universal Time的简写, 虽然可以认为U ...

  2. PHP等值判断中,常量与变量在左在右的区别

    在平时写代码过程中,经常会用到判断变量与常量相等的情况,比如if($a==1){…..} 或者if(1==$a) ,从执行效率上来说,1==$a比 $a==1的效率更高. 这个写法的另一个好处,当使用 ...

  3. Idea安装lombok插件【转载】

    参照:http://www.cnblogs.com/holten/p/5729226.html https://yq.aliyun.com/articles/59972 lombok是一个可以通过简单 ...

  4. zabbix 本地编译安装

    转载地址 https://www.cnblogs.com/wangxiaoqiangs/p/5412147.html 简介: 单独整理一下 Zabbix Agent . 1.安装包选择 下载地址:ht ...

  5. Linux网络编程学习(十) ----- Socket(第六章)

    前言:由于第五章主要介绍了TCP和UDP协议以及两者的包头的字段以及相应的功能,这里就不介绍了,对着字段看功能就好了,后续开始学习第六章 1.Socket Socket实质上就是提供了通信的端点,每个 ...

  6. 本地jar包添加至Maven仓库

    Maven命令将本地的jar包方放到maven仓库中 //自定义本地的jar包在pom文件的参数 <dependency> <groupId>com.eee</group ...

  7. WCF基础_使用svcutil.exe 工具来生成调用文件

    右键单击一个服务在浏览器中打开时,通常会有这么一段话: ServiceDemo 服务 已创建服务. 若要测试此服务,需要创建一个客户端,并将其用于调用该服务.可以使用下列语法,从命令行中使用 svcu ...

  8. java 诊断工具——Arthas

    该说不说!小编做的这些功能,最讨厌的就是优化!某些前辈大佬写的代码小辈我实在不敢恭维!那逻辑!那sql! 接下来!今天的主角就登场了,阿里巴巴最近开源出来的一个针对 java 的工具,主要是针对 ja ...

  9. 轮播插件swiper

    使用步骤 1.引用js <script src="swiper/swiper.min.js" type="text/javascript" charset ...

  10. 原型设计工具—Axure

    作为软件设计的一员,需要在软件开发初期确保软件的具体内容,防止后期大幅度的修改. 在这样的情况下,原型设计软件就起到了关键的作用. 摘要: 原型设计为什么这么重要呢? 因为它帮助我们搭建了低保真或高保 ...