将Matplotlib的strpdate2num与Python 3.2一起使用时发生TypeError [英] TypeError when using Matplotlib's strpdate2num with Python 3.2

查看:272
本文介绍了将Matplotlib的strpdate2num与Python 3.2一起使用时发生TypeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我当前的项目中,我想使用以下代码将文本文件中的一些实验数据读取到Python中:

In my current project I want to read some experimental data from a text file into Python using the following code:

import numpy as np
from matplotlib.dates import strpdate2num

data = np.recfromtxt('example.txt',
                 comments='#',
                 delimiter=';',
                 names=('time', 't_ref', 't_s', 't_amb1', 't_amb2', 't_amb3')
                 ,converters={'time': strpdate2num('"%d.%m.%Y %H:%M:%S"')}
                 )

example.txt看起来像

"04.10.2012 08:15:27";14.4;16;12.78;12.65;12.52
"04.10.2012 08:15:37";14.4;16;12.78;12.65;12.5
"04.10.2012 08:15:47";14.4;16;12.78;12.62;12.5
"04.10.2012 08:15:57";14.4;15.9;12.78;12.65;12.52
...

在Python 2.7中,一切都很好,但是当我尝试在3.2中传输代码时,我从strpdate2num()的说法中得到了TypeError

In Python 2.7, all is well, but when I try to transfer the code in 3.2, I get a TypeError from strpdate2num() saying

TypeError: strptime() argument 0 must be str, not <class 'bytes'>

我对Python相当陌生,但是我的理论是NumPy以某种方式在内部将时间数组存储为字节而不是字符串,这与自Python 3以来对两者的更严格处理相冲突.

I am fairly new to Python, but my theory is that NumPy somehow stores the time-array internally as byte instead of string, which collides with the stricter handling of both since Python 3.

长话短说,您是否知道解决该问题的原因是什么?

Long story short, do you have any idea what might be the cause how to fix that?

推荐答案

unutbu 的变通办法可以很好地工作.同时,似乎已经解决了问题.对我来说,使用bytespdate2num()代替strpdate2num()可以.

The workaround of unutbu work perfectly fine. Meanwhile it seems that the issue has been addressed. Using bytespdate2num() instead of strpdate2num() works for me.

这篇关于将Matplotlib的strpdate2num与Python 3.2一起使用时发生TypeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆