我正在将一个脚本从Python 2转移到Python 3。
我使用以下代码来检查RPM包。
import rpm
TS=rpm.TransactionSet()
CHECKPACKAGE=TS.dbMatch('name', 'gpm')
for h in CHECKPACKAGE:
print("%s-%s-%s" % (h['name'], h['version'], h['release']))
if (h['version'] == "1.20.7") and (h['release'] == "7.60"):
print("=> check gpm: version gpm is in sync")
print("")
else:
print("!gpm: version gpm is NOT in sync please check!")
print("")使用Python 2,我得到了
gpm-1.20.7-7.60
=> check gpm: version gpm is in sync使用Python 3,我得到了
b'gpm'-b'1.20.7'-b'7.60'
!gpm: version gpm is NOT in sync please check!我可以做些什么来获得与Python3相同的结果?
问候
发布于 2019-12-17 23:52:40
在h['release']和h['version']上调用方法decode()
if (h['version'].decode() == "1.20.7") and (h['release'].decode() == "7.60"):
...它看起来像是一个字节字符串,而不是字符串。
你可以通过比较gpm-1.20.7-7.60和b'gpm'-b'1.20.7'-b'7.60'来理解它。
decode()将删除b''。
https://stackoverflow.com/questions/59377703
复制相似问题