views:

1132

answers:

3

Hi,

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:

SyntaxError: Non-ASCII character '\xa3' in file dbscan.py on line 253, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

OK. So I looked at that PEP, and now tried adding this to the beginning of my script:

# coding=cp437

That stopped the error, but the output shows ú where it should show £.

I've tried ISO-8859-1 as well, with the same result.

Does anyone know which encoding I need?

Or where I could look to find out?

Thanks,

Ben

+2  A: 

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.

unwind
Ah! Thank you. So now I have to figure out what encoding to use in a Windows dos-box...
Ben
Your edit has solved my problem! I get a £ sign by printing \x9c, without specifying any encoding. Weird, but fine by me! :-)
Ben
Your terminal seems to emulate DOS, so it's CP437 or CP850.
vartec
A: 

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"
rjmunro
A: 

Hi,

try the encoding :

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

and then to display the '£' sign:

print unichr(163)
djibril