Syntaxerror: Non-Ascii Character '\Xa3' in File When Function Returns '£'

SyntaxError: Non-ASCII character but no encoding declared

Try adding

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

at the beginning of the file,
make sure your file is really encoded in utf-8

SyntaxError of Non-ASCII character

You should define source code encoding, add this to the top of your script:

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

The reason why it works differently in console and in the IDE is, likely, because of different default encodings set. You can check it by running:

import sys
print sys.getdefaultencoding()

Also see:

  • Why declare unicode by string in python?
  • Changing default encoding of Python?
  • Correct way to define Python source code encoding

Python: How to solve SyntaxError: Non-ASCII character?

You file is saved as UTF-16 with BOM (big-endian). I saved your sample code in Notepad++ with that encoding and reproduced the error:

  File "test.py", line 1
SyntaxError: Non-ASCII character '\xfe' in file x.py on line 1, but no encoding declared; see http://python.org/dev/peps
/pep-0263/ for details

Make sure your file is saved in the encoding declared. You have to use an encoding compatible with ASCII for the hash bang and encoding lines to be read properly. UTF-16 is not compatible, hence the error message when it read the non-ASCII bytes of the byte order mark (BOM) character.

Python SyntaxError: Non-ASCII character '\xe2' in file

You've got a stray byte floating around. You can find it by running

with open("x.py") as fp:
for i, line in enumerate(fp):
if "\xe2" in line:
print i, repr(line)

where you should replace "x.py" by the name of your program. You'll see the line number and the offending line(s). For example, after inserting that byte arbitrarily, I got:

4 "\xe2        lb = conn.create_load_balancer('my_lb', ['us-east-1a', 'us-east-1b'],[(80, 8080, 'http'), (443, 8443, 'tcp')])\n"

SyntaxError: Non-ASCII character '\xff' when running C executable using subprocess

sys.executable is the Python interpreter. Sys hello_C is a compiled C program, not a Python script, you shouldn't use it to run the program. Just run the program directly.

p = Popen(["./hello_C"], stdout=PIPE, bufsize=1)


Related Topics



Leave a reply



Submit