What encoding do I need to display a GBP sign (pound sign) using python on cygwin in Windows XP?

后端 未结 3 945
粉色の甜心
粉色の甜心 2021-01-18 00:31

I have a python (2.5.4) script which I run in cygwin (in a DOS box on Windows XP). I want to include a pound sign (£) in the output. If I do so, I get this error:

         


        
相关标签:
3条回答
  • 2021-01-18 00:41

    There are two encodings involved here:

    • The encoding of your source code, which must be correct in order for your input file to mean what you think it means
    • The encoding of the output, which must be correct in order for the symbols emitted to display as expected.

    It seems your output encoding is off now. If this is running in a terminal window in Cygwin, it is that terminal's encoding that you need to match.

    EDIT: I just ran the following Python program in a (native) Windows XP terminal window, thought it was slightly interesting:

    >>> ord("£")
    156
    

    156 is certainly not the codepoint for the pound sign in the Latin1 encoding you tried. It doesn't seem to be in Window's Codepage 1252 either, which I would expect my terminal to use ... Weird.

    0 讨论(0)
  • 2021-01-18 00:56

    try the encoding :

    # -*- coding: utf-8 -*-

    and then to display the '£' sign:

    print unichr(163)
    
    0 讨论(0)
  • 2021-01-18 00:57

    The Unicode for a pound sign is 163 (decimal) or A3 in hex, so the following should work regardless of the encoding of your script, as long as the output encoding is working correctly.

    print u"\xA3"
    
    0 讨论(0)
提交回复
热议问题