[melbourne-pug] Unicode for windows dummies

Mike Dewhirst miked at dewhirst.com.au
Tue Aug 16 01:09:30 EDT 2016


On 16/08/2016 2:57 PM, William ML Leslie wrote:
> On 16 August 2016 at 14:40, Anthony Briggs <anthony.briggs at gmail.com> wrote:
>> print("M├┐ h├┤v├¿r├ºr├áft ├«├ƒ f├╗┼él ├Âf ├®├¬l┼ø")
>>
>> works just fine for me, since you're just printing an internal Python
>> string.
>
> It will work fine unless you're on Mike's machine - if
> sys.stdout.encoding is cp850 and you've got unicode_literals imported
> (or are using python3), it won't.

Ok. I'm on Mike's machine and I'm using Python 3.5 ...

But I just discovered a command >chcp 1252 which switches the active 
code page and sys.stdout respects that.

>
>> The problem is from trying to print a binary string (which is what
>> you get from .encode()) as an internal Python string. If you specify an
>> encoding, the error goes away:
>>
>> print("M├┐ h├┤v├¿r├ºr├áft ├«├ƒ f├╗┼él ├Âf
>> ├®├¬l┼ø".encode("utf-8").decode("cp1252", "replace"))
>
> The only reason to encode to utf-8 and then decode from cp1252 is to
> fix incorrect input.
>
> I think you mean .encode("cp1252", "replace").decode("cp1252")
>



More information about the melbourne-pug mailing list