>
> If that were decoded using latin1 how would I then get the first two bytes
> to the integer 256 and the last six bytes to their Cyrillic meaning?
> (Apologies for not testing myself, short on time.)

I cannot conceive why you would. Slice the bytes then use
struct.unpack on the first 2 bytes and decode on the last 6.

exactly.
 
">

(original) (raw)

On Thu, Jan 9, 2014 at 2:54 PM, Paul Moore
> For example: b'\\x01\\x00\\xd1\\x80\\xd1\\83\\xd0\\x80'
\>
\> If that were decoded using latin1 how would I then get the first two bytes
\> to the integer 256 and the last six bytes to their Cyrillic meaning?
\> (Apologies for not testing myself, short on time.)

I cannot conceive why you would. Slice the bytes then use
struct.unpack on the first 2 bytes and decode on the last 6.

exactly.

We're

talking about using latin1 for cases where you want to treat the text

as essentially ascii (with a few bits of binary junk you want to ignore).


as so -- I want to replace a bit of ascii text surrounded by arbitrary binary:
(apologies for the py2...)


In \[24\]: b
Out\[24\]: '\\x01\\x00\\xd1\\x80\\xd1a name\\xd0\\x80'

In \[25\]: u = b.decode('latin-1')

In \[26\]: u2 = u.replace('a name', 'a different name')

In \[28\]: b2 = u2.encode('latin-1')

In \[29\]: b2
Out\[29\]: '\\x01\\x00\\xd1\\x80\\xd1a different name\\xd0\\x80'

-Chris





Please don't take away the message that latin1 makes things

"just like Python 2.X" - that's completely the wrong idea.



Paul




--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax

Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov