2

I want to concatenate two strings like this:

requestData = command + ' ' + data

"data" in my case holds binary data, that should not be opened - it should just glue it to command. But imho python is attempting to open it and it fails with:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xbc in position 1: ordinal not in range(128)

Is there a way to glue it without opening?

Edit: Python 2.7 Also my data is actualy not utf-8 decode might not help - its binary data.

4
  • Which Python version is this? Must be 2.something, but Python 2.7.3 successfully concatenates non-ascii strings: In [1]: 'привет' + ' ' + 'Veseliq' Out[1]: '\xd0\xbf\xd1\x80\xd0\xb8\xd0\xb2\xd0\xb5\xd1\x82 Veseliq'. Commented May 15, 2012 at 9:44
  • Its python 2.7, and the data is misc binary data Commented May 15, 2012 at 9:51
  • 1
    Is command unicode? If yes, encode it first. Commented May 15, 2012 at 10:05
  • how do you initalize your command ? Commented May 15, 2012 at 10:51

1 Answer 1

4

Try using http://docs.python.org/library/array.html (with 'B') instead of string

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

1 Comment

Sure - it appears that you never intended to use the data as a printable string (in which case I would have recommended to change the encoding used from ASCII to a unicode representation). In that case using string makes little sense and can cause unexpected problems. My suggestion was to use a type that properly represents your intent, and then concatenating items of that type (e.g. using docs.python.org/library/array.html#array.array.extend), 'B' represents unsigned char, which is equivalent to one byte, which seems to be the proper representation if I understand your needs correctly.

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.