EDIT: To save people the trouble of scrolling, the problem stemmed from the "decode" operation needing an output variable; my script failed to do this. I thought that the "for" loop would modify the variable in-situ, but this isn't the case.
To make a long story short, I have some netCDF files from which I am generating a series of maps. The script for that works fine, but I am having major issues getting the title to display correctly. I get the variable from the netCDF file that will act as my title (a simple timestamp, basically). First I tried making it a Python variable, then using it as the plot title.
Unfortunately, I have learned that it is what is known as a "bytes" string. Which means that the title has a bunch of lowercase 'b's in front of them. Not just one at the start. ie:
b'T' b'i' b't' b'l' b'e'
This is because the netCDF variable is a masked array. I managed to get some workable code to convert that array into a list, then into a string, and everything seemed like it would work. The linchpin of the entire thing, however, is the "bytes.decode()" operation.
As far as I understand it, this operation takes in bytes objects, and then returns them as plain strings. Afaik, these are in utf-8, and I checked the type going in and found they were all classed as "bytes". Yet, when I attempt to use decode, it tells me that the objects are not bytes, literally moments after it told me they were? See code below and the output/error.
Code:
#check the type, shape, and data of times
print(type(times))
print(times.shape)
print(times.data)
#change the times masked array to a list
timeslist = times.tolist(fill_value=-9999)
#check to see if elements of the list are bytes
for x in timeslist:
print(type(x))
#new list for decoded chars
fixedtimeslist = []
#decode the bytes list
for x in timeslist:
bytes.decode('utf-8')
fixedtimeslist.append(x)
Output/Error:
<class 'numpy.ma.core.MaskedArray'>
(19,)
[b'2' b'0' b'1' b'2' b'-' b'1' b'0' b'-' b'0' b'4' b'_' b'0' b'3' b':' b'0' b'0' b':' b'0' b'0']
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
Traceback (most recent call last):
File "Wind10.py", line 82, in <module>
bytes.decode('utf-8')
TypeError: descriptor 'decode' requires a 'bytes' object but received a 'str'
EDIT: A few people have asked, and yes I have tried doing this using "x.decode" an iteration ago. When I do that instead, and re-check the type, it remains as bytes.
Code:
#decode the bytes list
for x in timeslist:
x.decode('utf-8')
fixedtimeslist.append(x)
#recheck to see if decode worked
for x in fixedtimeslist:
print(type(x))
Output:
(19,)
[b'2' b'0' b'1' b'2' b'-' b'1' b'0' b'-' b'0' b'4' b'_' b'0' b'3' b':' b'0' b'0' b':' b'0' b'0']
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
So I'm kind of at a loss for what to do with this problem. I don't know if I'm just not understanding something in the semantics or I found a bug or what.
I realize that questions similar to this have been asked, and I have seen them, and tried to emulate their solutions with no success. This is the 4th or 5th program iteration I've tried. Either the decode seems to do nothing at all (ie: the string still has the b'' part), or I get this error.
If it matters, I'm using Python 3.6 miniconda on CentOS 6.8 I think.
Any and all help is appreciated! I apologize if this is trivial; I'm no computer scientist.