关于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. 关于background-size 的一点小坑

    目前在做一个网页,要求使用一个图片作为背景,必须要全屏目填充,于是我想,这不是很简单的事情么: 有两种实现方法:首先设置添加一个div(或者body),把div(或者body)元素的宽和高都设置成10 ...

  2. docker 启动报错 WARNING: IPv4 forwarding is disabled. Networking will not work.

    解决办法: # vi /etc/sysctl.conf 或者 # vi /usr/lib/sysctl.d/00-system.conf 添加如下代码:     net.ipv4.ip_forward ...

  3. webservice 教程

    https://ke.qq.com/webcourse/index.html#cid=28875&term_id=100182700&taid=800324205965515& ...

  4. Redis 编译安装

    系统学习一下,记录一下笔记,之前都是断断续续尝试过一些简单的安装使用 下载,解压 编译安装 copy配置文件 启动连接 ./bin/redis-server ./redis.conf 登陆./bin/ ...

  5. [原创]如果软件在网络磁盘中或移动磁盘中运行时需要解决 exception C0000006 异常问题

    //如果软件在网络磁盘中或移动磁盘中运行时需要利用下面这句命令来解决 exception C0000006 异常问题 {$SetPEFlags IMAGE_FILE_REMOVABLE_RUN_FRO ...

  6. 函数function

    function add(x,y,z){ sum = x + y +z; document.write(x+"+"+y+"+"+z+"="+ ...

  7. 求$N^N$的首位数字

    正如"大得多"定理所言,当$n\longrightarrow \infty$时: $$  n^n \gg n! \gg a^n \gg n^b \gg ln^kn $$ $f(n) = n^n$的增长 ...

  8. validate表单验证

    validate使用步骤:1.导入jquery.js2.导入validate.js3.在页面加载成功之后 对表单进行校验  $("选择器").validate()4.在valida ...

  9. Tomcat的三种安装方式:解压版、安装版、配置成Windows服务版

    https://blog.csdn.net/Jessica_XLF/article/details/81711429

  10. linux卸载erlang

    rpm -qa | grep erlang | xargs rpm -e --nodeps