关于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. mongodb集群部署

    一.安装Mongodb(Tarballs) 1.检查依赖文件包: yum install libcurl openssl 2.解压文件到/usr/local/ tar -zxvf mongodb-li ...

  2. 如何高效的学习Java开发

    IT 行业的变化快是众人皆知的,需要持续去学习新的知识内容.但是,往往我们工作之后,经常发现学习的东西很少了,学习效率非常低,感觉自己到了一个瓶颈期,久而久之,就演变成『一年工作经验,重复去用十年』的 ...

  3. pycharm+Django+MySQL项目连接数据库中原有数据库

    1.安装驱动 一般会有MySQLdb.pymysql等,因为python版本等问题,我安装的是pymysql. 还可以在项目里安装,File->settings->project 2.在项 ...

  4. linux命令行下xlsx转换成pdf或csv的笔记

    使用libreoffice(可以用yum直接安装,占用了4xxM磁盘空间...) 然后命令行执行: 转换成csv,支持utf-8中文编码: libreoffice  --invisible --con ...

  5. tornado 基于MongoDB存储 session组件开发

    1.开发伊始 根据源码中RequestHandler类中发现__init__函数中会调用自身initialize函数,此函数中为pass,即可以围绕initialize开发一系列的组件 2.开发实现 ...

  6. 在同时满足if 和 else 条件的情况下,输出所需的内容。

    请问划线处填什么内容,可以输出 "Hello World!"?if(__________){    printf("Hello ");}else{    pri ...

  7. C#使用Owin技术部署轻量级webApi服务

    写在前面: 除了使用IIS可以启用WebApi之外,微软还提供了Owin技术,免除了IIS繁琐的部署配置,只需要运行编写好的程序,即可启用webApi服务,是不是很爽呢? 对于Owin技术的详细介绍这 ...

  8. vue props命名最好直接使用kebab-case (短横线隔开式) 命名

    vue官方解释,props使用驼峰命名的话,在html中使用时也需要转化成短横线命名,否则报错.个人比较喜欢上下文统一,直接在props命名时就把它命名成短横线隔开式吧.

  9. Linux下普通IO文件操作函数---C语言

    普通文件IO总结 FILE结构体    typedef struct   {       int level; /*填充/清空一级缓存*/     unsigned flag; /*文件状态指针*/ ...

  10. webpack and publish lib

    http://keer2345.github.io/2018/04/13/webpack-4-tutorial-example-with-npm/ https://blog.csdn.net/feng ...