关于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 ...
随机推荐
- 关于background-size 的一点小坑
目前在做一个网页,要求使用一个图片作为背景,必须要全屏目填充,于是我想,这不是很简单的事情么: 有两种实现方法:首先设置添加一个div(或者body),把div(或者body)元素的宽和高都设置成10 ...
- 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 ...
- webservice 教程
https://ke.qq.com/webcourse/index.html#cid=28875&term_id=100182700&taid=800324205965515& ...
- Redis 编译安装
系统学习一下,记录一下笔记,之前都是断断续续尝试过一些简单的安装使用 下载,解压 编译安装 copy配置文件 启动连接 ./bin/redis-server ./redis.conf 登陆./bin/ ...
- [原创]如果软件在网络磁盘中或移动磁盘中运行时需要解决 exception C0000006 异常问题
//如果软件在网络磁盘中或移动磁盘中运行时需要利用下面这句命令来解决 exception C0000006 异常问题 {$SetPEFlags IMAGE_FILE_REMOVABLE_RUN_FRO ...
- 函数function
function add(x,y,z){ sum = x + y +z; document.write(x+"+"+y+"+"+z+"="+ ...
- 求$N^N$的首位数字
正如"大得多"定理所言,当$n\longrightarrow \infty$时: $$ n^n \gg n! \gg a^n \gg n^b \gg ln^kn $$ $f(n) = n^n$的增长 ...
- validate表单验证
validate使用步骤:1.导入jquery.js2.导入validate.js3.在页面加载成功之后 对表单进行校验 $("选择器").validate()4.在valida ...
- Tomcat的三种安装方式:解压版、安装版、配置成Windows服务版
https://blog.csdn.net/Jessica_XLF/article/details/81711429
- linux卸载erlang
rpm -qa | grep erlang | xargs rpm -e --nodeps