我有一个名为PySide2.QtCore.QByteArray的roleName对象,它编码了一个python字符串:
propName = metaProp.name() // this is call of [const char *QMetaProperty::name() ](https://doc.qt.io/qt-5/qmetaproperty.html#name)
// encode the object
roleName = QByteArray(propName.encode())
print(roleName) // this gives b'myname'
// now I would like to get just "myname" without the "b"
roleString = str(roleName)
print(roleString) // this gives the same output as above我怎么才能拿回我的解码字符串?
发布于 2019-08-26 22:00:22
在Python3中,当将类似字节的对象转换为文本字符串时,必须指定编码。在PySide/PyQt中,这适用于QByteArray,就像它适用于bytes一样。如果不指定和编码,str()的工作方式类似于repr():
>>> ba = Qt.QByteArray(b'foo')
>>> str(ba)
"b'foo'"
>>> b = b'foo'
>>> str(b)
"b'foo'"有几种转换为文本字符串的不同方法:
>>> str(ba, 'utf-8') # explicit encoding
'foo'
>>> bytes(ba).decode() # default utf-8 encoding
'foo'
>>> ba.data().decode() # default utf-8 encoding
'foo'最后一个例子是特定于QByteArray的,但是前两个应该可以处理任何类似字节的对象。
https://stackoverflow.com/questions/57663191
复制相似问题