Reputation: 25
I am transfering a script from Python 2 to Python 3.
I am using the following code to check an RPM package.
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("")
With Python 2 I got
gpm-1.20.7-7.60
=> check gpm: version gpm is in sync
With Python 3 I got
b'gpm'-b'1.20.7'-b'7.60'
!!!check gpm: version gpm is NOT in sync please check!!!
What can I do to have the same result with Python3?
Regards
Upvotes: 0
Views: 211
Reputation: 1802
Call method decode()
on h['release']
and h['version']
:
if (h['version'].decode() == "1.20.7") and (h['release'].decode() == "7.60"):
...
It looks like you have a byte string instead of a string.
You can understand it comparing gpm-1.20.7-7.60
and b'gpm'-b'1.20.7'-b'7.60'
.
decode()
will remove b''
.
Upvotes: 1