关于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. Django 2.0 新款URL配置详解

    Django2.0发布后,很多人都拥抱变化,加入了2的行列.但是和1.11相比,2.0在url的使用方面发生了很大的变化,下面介绍一下: 一.实例 先看一个例子: from django.urls i ...

  2. Cookie在前端读不到 多半是因为Cookie在服务器端的设置是HttpOnly 意味着只能在后台操作Cookie

    比如Shiro框架的 RememberMe Cookie 是不允许Js进行读写的 只能在服务器端通过同一个域的请求获得 import com.constantine.forum.exception.F ...

  3. python_14 多态,封装

    多态: 由不同的类实例化得到的对象,调用同一个方法,执行的逻辑不同. 多态的概念指出了对象如何通过他们的共同的属性和动作来操作及访问,而不需考虑他们的类. class H2O: def __init_ ...

  4. 使用Idea从github上获取项目

    转载自:https://www.cnblogs.com/30go/p/7909246.html 整体分三步: 下载和安装git 配置idea 从git获取项目 详细步骤: 1. 下载和安装git 下载 ...

  5. Why Everyone Should Lift Weights

    Why Everyone Should Lift Weights by James Clear I'll say it plain and simple: you should be lifting ...

  6. 如何解决 快速点击多次触发的bug 期望快速点击只一次生效

    var lastClick; lockClick(){ var nowClick = new Date(); if (lastClick === undefined) { lastClick = no ...

  7. UE4 Virtual Reality Input输入配置表导入

    [/Script/Engine.InputSettings] AxisConfig=(AxisKeyName="OculusTouch_Right_FaceButton2",Axi ...

  8. lanya

    var app = getApp()   Page({   data: {   motto: 'Hello World',   openBLE:'打开蓝牙设备',   startBLEDiscover ...

  9. winfrom中pictureBox控件的部分使用方法

    一.后台属性 1.pictureBox1.Image显示图片 2.pictureBox1.ImageLocation存储和提取图片路径 二.面板属性 1.Picturebox控件SizeMode属性 ...

  10. openpyxl一点心得

    先上代码 from openpyxl import workbook,load_workbook class HomeWork(): def creat_xlsx(self): "" ...