PythonRPM在python3和python2之间显示不同的结果

2024-06-28 20:03:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在将一个脚本从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("!!!check 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'
!!!check gpm: version gpm is NOT in sync please check!!!

我该怎么做才能使Python3获得相同的结果

问候


Tags: namein脚本releaseisversionchecknot
1条回答
网友
1楼 · 发布于 2024-06-28 20:03:08

h['release']h['version']上调用方法decode()

if (h['version'].decode() == "1.20.7") and (h['release'].decode() == "7.60"):
...

看起来是字节字符串而不是字符串

你可以通过比较gpm-1.20.7-7.60b'gpm'-b'1.20.7'-b'7.60'来理解它

decode()将删除b''

相关问题 更多 >