摆脱';b';在Python字符串前面

摆脱';b';在Python字符串前面,python,string,byte,python-3.6,netcdf,Python,String,Byte,Python 3.6,Netcdf,编辑:为了避免人们滚动的麻烦,问题源于需要输出变量的“解码”操作;我的脚本没有做到这一点。我以为“for”循环会在原地修改变量,但事实并非如此 长话短说,我有一些netCDF文件,从中生成一系列地图。该脚本运行良好,但我在正确显示标题方面遇到了重大问题。我从netCDF文件中获取变量,该文件将作为我的标题(基本上是一个简单的时间戳)。首先,我尝试将其设置为Python变量,然后将其用作绘图标题 不幸的是,我知道这是一个被称为“字节”的字符串。这意味着标题前面有一堆小写字母“b”。不仅仅是一开始。

编辑:为了避免人们滚动的麻烦,问题源于需要输出变量的“解码”操作;我的脚本没有做到这一点。我以为“for”循环会在原地修改变量,但事实并非如此

长话短说,我有一些netCDF文件,从中生成一系列地图。该脚本运行良好,但我在正确显示标题方面遇到了重大问题。我从netCDF文件中获取变量,该文件将作为我的标题(基本上是一个简单的时间戳)。首先,我尝试将其设置为Python变量,然后将其用作绘图标题

不幸的是,我知道这是一个被称为“字节”的字符串。这意味着标题前面有一堆小写字母“b”。不仅仅是一开始。即:

b'b'i'b'T'b'l'b'e'

这是因为netCDF变量是一个屏蔽数组。我设法得到了一些可行的代码,将数组转换成一个列表,然后再转换成一个字符串,一切看起来都可以正常工作。然而,整个过程的关键是“bytes.decode()”操作

据我所知,这个操作接受字节对象,然后将它们作为普通字符串返回。好了,这些是utf-8格式的,我检查了输入的类型,发现它们都被归类为“字节”。 然而,当我尝试使用decode时,它告诉我对象不是字节,在它告诉我它们是字节之后的几分钟?请参阅下面的代码和输出/错误

代码:

输出/错误:

<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'
输出:

(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'>
(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'0'b']
所以我有点不知所措,不知该怎么处理这个问题。我不知道我是否只是不理解语义,或者我发现了一个bug或者什么

我意识到有人提出过类似的问题,我也看到过,并试图模仿他们的解决方案,但没有成功。这是我尝试过的第四次或第五次程序迭代。要么解码似乎什么都不做(即:字符串仍然有b''部分),要么我得到了这个错误

如果有必要的话,我想我正在CentOS 6.8上使用Python 3.6 miniconda


感谢您的任何帮助!我道歉,如果这是微不足道的;我不是计算机科学家。

我想你的意思是
x.decode('utf-8')
。我对字节对象没有任何经验,但我相信这是您想要的

您必须将
x
解码为字节,并将返回值附加到
fixedtimeslist
列表中

for x in timeslist:
    fixedtimeslist.append(x.decode('utf-8'))

看起来你有输入错误,不应该是
bytes.decode('utf-8')
x=x.decode('utf-8')
?您试图解码列表中的每个元素,并将其结果附加到列表中,对吗?我也尝试过,但它似乎没有任何作用。没有错误,并且在重新检查类型时,它仍然会说它们是字节。从字节解码某些内容会返回字符串。返回。你必须把它放入变量中。Decodig不会修改初始变量。在您的代码中,您将初始变量放入列表中,当然,列表中再次只包含字节。请将此特定代码段更改为
bytes.decode('utf-8')
,精确为
x=x.decode('utf-8')
(听起来你根本没有试过。正如@dgan所提到的,你实际上需要对bytestring解码的结果做些什么。x.decode('utf-8')只会创建一个被抛出的未命名变量。再次,将此行
bytes.decode('utf-8')
更改为
x=x.decode('utf-8'))
。看起来你根本没有尝试过这个。等等,当我有for循环时我想:“对于timeslist中的x:x.decode('utf-8')fixedtimeslist.append(x)”,我在附加“fixed”值?我也试过了,但似乎什么都没做。没有错误,在重新检查类型后,它仍然说它们是字节。这与上面其他人的建议类似。我想它只会通过“for”循环工作,在运行时更改x,但我想情况并非如此。谢谢你的时间。
(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'>
for x in timeslist:
    fixedtimeslist.append(x.decode('utf-8'))