关于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 ...
随机推荐
- pymssql包安装方法
https://docs.microsoft.com/en-us/sql/connect/python/pymssql/python-sql-driver-pymssql
- C++的正则
C++的正则封装的不丰富.只有最基础的三个主要的函数(也可能是我孤陋寡闻).要有更为丰富的功能需要自己进一步组合. 我目前只需要循环查找这个功能,并且我也不知道c++的正则支持正则的哪些功能; 代码如 ...
- logging-----日志模块
import logging #creat logger 第一步,创建一个记录器 logging_name = 'test' logger = logging.getLogger(logging_na ...
- CamStar insitexmlclient重新封装为.net Core类库
工作原因经常使用camstar的 InsiteXMLClient类库做二次开发,但是只能在4.X环境下使用,对于日益繁荣的.net core生态,花费了些时间把原有的类库重新封装为.net core ...
- vue中less文件全局引用
1.先安装sass-resources-loader npm install sass-resources-loader 2.然后在build->utils.js修改less配置 在less ...
- 【原创】qlogic网卡软中断不均衡问题分析
引子 使用qlogic QL45000网卡测试业务性能,发现cpu软中断分布不均衡,而且很有规律,导致cpu空闲也不是很均衡, 会影响业务稳定性. 设备使用3张网卡Qlogic网卡,配置为4*25G模 ...
- Excel VBA ——如何导出数据到excel表格
sub OutPut() Dim FileTitle, MyPath, MyFullName As String Application.ScreenUpdating = false '关闭表格公式的 ...
- 复杂JSON对象的查询与合并
一个表里存放了全国各地地区.省.市.县区的数据,为了提高加载速度我保存成了本地的JSON文件 结构大致如下: [{ "text": "中华人民共和国", &qu ...
- Android 获取版本号名称工具类
package com.example.grenaderose.redthunder.utils; import android.content.Context; import android.con ...
- CentOS 6安装配置mongodb
安装过程 服务器下载安装包 下载: curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-4.0.6.tgz; 解压 ...