0

I have a function using popen() that returns stdout as an array.

using Python3 the arrays are returned correctly, but under python 2.7 each element is prefixed with a 'u'

def exe(cmd):
    from subprocess import Popen, PIPE, STDOUT
    p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
    arr = p.stdout.read().decode().split("\n")
    print(arr)
    del arr[-1]
    if(arr[0]=='not found.'): arr = [];
    return arr

[u'10-000801d5a12d', u'']

Where is this 'u' coming from and how do I prevent it ?

2
  • You don't prevent it. When you print the list you get the representation of the content, not the value. Commented Nov 3, 2013 at 12:12
  • 1
    Only partially tongue-in-cheek answer - prevent it by switching Python 3.x! As far as I am aware, the worst thing about pre-3.x Python was strings' unicode support. Commented Nov 3, 2013 at 12:27

4 Answers 4

1

The u indicates the string is a Unicode string. This means that the string has been decoded from an encoding, such as UTF-8 or ISO-8895-1, into a unambiguous type. If you print arr[0] it will be encoded back to your terminal without the u.

This would have happened during the .read().decode(). You should really pass the proper file encoding to decode otherwise a system one could be used which may not be appropriate. If your file contains just ASCII chars then you won't see a problem.

The reason why it's different is because in Python 3, normal strings (decoded) are always Unicode strings whereas byte strings (from .read()) are a special type.

If you really don't want the u, remove the .decode() method but you really don't need to.

Sign up to request clarification or add additional context in comments.

Comments

1

The u stands for unicode to indicate it is a unicode string when printed. It doesn't affect the array contents in any way and is merely an indication

Comments

1

u'10-000801d5a12d' means '10-000801d5a12d' is a unicode string.

You can see it here at python docs

Use:

str(myString)

You can also use encode if you need specific encoding:

"myString".encode('utf-8')

Comments

0

The u means the string is a unicode string. If you really want a python str, you can cast it like that:

str(my_var)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.