Move to py3 problem

This is the place for queries that don't fit in any of the other categories.

Move to py3 problem

Postby pnelsonsr » Fri May 09, 2014 5:33 pm

I'm biting the bullet and moving a bunch of code to py3.4 and while this has gone rather well (amazingly enough!) there are a few things that I've had to hack around.
Heres one:
in py2.7.6
Code: Select all
import pexpect
branch=pexpect.run('git rev-parse --abbrev-ref HEAD').rstrip()
print branch
>>>development

in py3.4
Code: Select all
import pexpect
branch=pexpect.run('git rev-parse --abbrev-ref HEAD').rstrip()
print(branch)
>>>b'development'

What is the 'b' and how do I get rid of it? One thing I've done is:
in py3.4
Code: Select all
import pexpect
branch=str(e.run('git rev-parse --abbrev-ref HEAD').rstrip())[2:-1]
print(branch)
>>>development

But this feels dirty...

Anyone have a suggestion?
pnelsonsr
 
Posts: 25
Joined: Thu Dec 12, 2013 12:48 am

Re: Move to py3 problem

Postby stranac » Fri May 09, 2014 8:02 pm

pnelsonsr wrote:What is the 'b' and how do I get rid of it?

It means pexpect.run() a bytestring.
It's pretty much the same as your python 2 result, except python 2 doesn't need to show the b, since all strings are bytestrings by default.

If you want to convert it to a python 3 string (a unicode string), you can use the .decode() method.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1115
Joined: Thu Feb 07, 2013 3:42 pm

Re: Move to py3 problem

Postby pnelsonsr » Fri May 09, 2014 10:35 pm

Ah...

You're right py2 is the same and it's the py3 print function that is doing things a bit different. I found some good info about bytestrings and appreciate your pointing me in the right direction.
pnelsonsr
 
Posts: 25
Joined: Thu Dec 12, 2013 12:48 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 4 guests