1

How do I declare an encoding for this code?

I know how to declare encoding in other situations but how would I do it with this?

if used_prefix and cmd=="shoot" and user.name in whitelist:
    name = args.lower()
    if name in killed:
        room.message(user.name+' is already dead.')
    else:
        killed.append(name)
        saveKills()
        room.message('//'+user.name+' shoots '+name+' in the head.')

The error message says:

SyntaxError: Non-ASCII character '\xe3' in file /home/stephen/downloads/inhaley/inhaley.py on line 6, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details.

Jeremy
  • 1
  • 85
  • 340
  • 366
inhaleyproject
  • 27
  • 1
  • 1
  • 3
  • Wow -- it's very negligent that a URL emitted by a built-in error message was broken when the Python website was updated. Fortunately, [the new URL is easy enough to find](http://legacy.python.org/dev/peps/pep-0263/). – Jeremy Jul 13 '14 at 01:06
  • 1
    Since the code you posted doesn't actually contain any non-ASCII codepoints, I'd suspect an exotic Unicode character snuck in; what line is line 6 here? – Martijn Pieters Jul 13 '14 at 01:22
  • @MartijnPieters It redirects to the PEP index, not to the specific PEP. That's not horrible, but it shouldn't have been much more work to do it properly. – Jeremy Jul 13 '14 at 01:22
  • possible duplicate of [Working with utf-8 encoding in Python source](http://stackoverflow.com/questions/6289474/working-with-utf-8-encoding-in-python-source) – Brad Koch Jul 13 '14 at 01:31
  • @BradKoch: *There is no need here* to use a source encoding; all that is needed is to remove a stray character as the code itself is obviously just ASCII. – Martijn Pieters Jul 13 '14 at 01:32
  • @JeremyBanks: ick, indeed, it redirects to the index. How awkward, I'll file a report. – Martijn Pieters Jul 13 '14 at 01:35
  • @JeremyBanks: the [error message was corrected](http://bugs.python.org/issue21789), but I think the redirect could be fixed too. – Martijn Pieters Jul 13 '14 at 01:37

3 Answers3

0

put #coding=utf-8 at the top of your script/file

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

Add

#!/usr/bin/env python
# -*- coding: utf-8 -*-

On the first 2 lines.

0

Add #coding=utf-8 at the top of your code, as such:

#coding=utf-8

#imports here
import math

print 'The encoding has been set!'
ZenOfPython
  • 891
  • 6
  • 15