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.

ddinchev
  • 33,683
  • 28
  • 88
  • 133

1 Answers1

4

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

Ofir
  • 8,194
  • 2
  • 29
  • 44
  • 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 http://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. – Ofir May 15 '12 at 13:09