TypeError when using Matplotlib strpdate2num with Python 3.2

In my current project, I want to read some experimental data from a text file in 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"')} ) 

with example.txt similar to

 "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 ... 

Everything is fine in Python 2.7, but when I try to pass the code to 3.2, I get a TypeError from strpdate2num() , saying

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

I'm new to Python, but my theory is that NumPy somehow stores a temporary array inside a byte instead of a string, which faces more stringent processing like Python 3.

In short, do you have any ideas what might be the reason for how to fix this?

+4
source share
3 answers

The unutbu workaround works fine. Meanwhile, the problem seems to be an issue . Using bytespdate2num() instead of strpdate2num() works for me.

+6
source

Here is a workaround:

 import numpy as np import matplotlib.dates as mdates def bytedate2num(fmt): def converter(b): return mdates.strpdate2num(fmt)(b.decode('ascii')) return converter date_converter = bytedate2num("%d.%m.%Y %H:%M:%S") data = np.recfromtxt('example.txt', comments='#', delimiter=';', names=('time', 't_ref', 't_s', 't_amb1', 't_amb2', 't_amb3'), converters={'time': date_converter}) 
+5
source

I had to remove quotes from the sample text. (using python3.4)

 ValueError: time data '"04.10.2012 08:15:27"' does not match format '%d.%m.%Y %H:%M:%S' 
-1
source

Source: https://habr.com/ru/post/1480216/


All Articles