On 16 August 2016 at 14:30, William ML Leslie <[email protected]>
wrote:

> On 16 August 2016 at 14:24, Anthony Briggs <[email protected]>
> wrote:
> > Hi Mike,
> >
> > I was just trying to solve a similar problem at the PyconAU sprints :)
> >
> > The error is that there are some things / Unicode strings which don't
> > translate to Windows 'charmap' characters, and can't be printed to the
> > terminal. You can replicate it with this code:
> >
> > print("M├┐ h├┤v├¿r├ºr├áft ├«├ƒ f├╗┼él ├Âf ├®├¬l┼ø".encode("cp1252"))
>
> The print() is redundant here, the function call is never reached;
> this is different to the example, where it is the print itself that is
> failing.
>
> print("M├┐ h├┤v├¿r├ºr├áft ├«├ƒ f├╗┼él ├Âf ├®├¬l┼ø")
>
> will also reproduce the error, but for a different reason: stdout now
> fails to encode.
>

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. 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"))

which is not what you would do in practice, but the 'replace' will drive
over anything not displayable on a terminal, so  ¯\_(ツ)_/¯

I also forgot the fourth option, which is to specify the encoding when
opening the file, eg. file('blah.csv', 'w', encoding='utf-8')

Anthony
_______________________________________________
melbourne-pug mailing list
[email protected]
https://mail.python.org/mailman/listinfo/melbourne-pug

Reply via email to