"SyntaxError: Non-ASCII character" in running Python code - python

Im starting with python and when i use the interpreter and run this code:
>>>peliculas = ["movie1", "movie2", "movie3", "movie4"]
>>>print(peliculas[1])
when i use Pycharm IDE it doesnt compile:
peliculas = ["movie1", "movie2", "movie3", "movie4"]
print(peliculas[1])
This is the output:
File "/Users/user/PycharmProjects/untitled/Prueba2.py", line 1
SyntaxError: Non-ASCII character '\xc3' in file /Users/user/PycharmProjects/untitled/Prueba2.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
What´s wrong with python?.. do i installed it correctly?

Just add these two lines at to top of python code.
#!/usr/bin/env python
# -*- coding: utf-8 -*-

It's a file encoding problem. Try adding this at the beginning of the file:
#coding=utf-8

In PyCharm you can specify file encoding via the File | File encoding menu, or the file encoding item on the status bar. See this help article

Related

Python 2.7: How to print this character?

I want to print this character "¥" in python 2.7.
Here is my code:
test = "¥"
print(test)
I execute this code and I get this error message:
SyntaxError: Non-ASCII character '\xc2' in file main.py on line 5, but
no encoding declared; see http://python.org/dev/peps/pep-0263/ for
details
How to print this character?
Thanks in advance
You need to specify the following line in the top of file:
# -*- coding: <encoding> -*-
Next time when you run this file Python will know what the encoding of that character is.
It will look like this, assuming you use utf-8.
# -*- coding: utf-8 -*-
test = "¥"
print(test)

code runs from interpreter but not in editor

I am trying to add some symbols to a text file,i can not define these symbols in editor
but it works from command line.
symbols = '$¢£¥€¤' works in interpreter but not editor(sublime),however it doesn't print these symbols correctly in command.However if i decode("utf-8") then print works fine.
symbols = '$¢£¥€¤'
s=symbols.decode("utf-8")
I use python 2.7 and sublime text editor
this is the error i get when i run using editor
SyntaxError: Non-ASCII character '\xc2' in file /home/programmer/Desktop/seleniumIns.py on line 184, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
How can i fix these to add them to my original program in editor
When you run a python file containing unicode, you need to tell the interpreter what encoding is used.
In your case put at the very first line of your script this line:
# -*- coding: utf-8 -*-
And you'll be able to use utf-8!

Non-ASCII character '\xe2' in file but no encoding declared

I wrote a script to extract signals from the MIT-BIH dataset using the wfdb python library. The script was working fine when I was running it on windows but I recently shifted to Mac. After installing all the dependencies I got an error when I tried to import processing from the wfdb library. This is the error I get:
SyntaxError: Non-ASCII character '\xe2' in file /usr/local/lib/python2.7/site-packages/scipy/stats/_continuous_distns.py on line 3346, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
import wfdb works fine but there seems to be a problem when I do from wfdb import processing. Is there any way to solve this issue?
Please add following line at the top of the code.
# -*- coding: utf-8 -*-
Also, avoid using non-ascii quotations.
This error is caused due to copying and pasting code from web which causes stray byte floating. You can find it by running.
with open('my_script.py', 'r') as ms:
for i, line in enumerate(ms):
if '\xe2' in line:
print(i, repr(line))
And the line and its index value will be printed where there is '\xe2':
4, "\xe2 word=string.printable(random.randint[0,61]) # Gets the random word"
Note: You should replace my_script.py with your respective .py file.
This was due to a scipy bug which has been fixed.

Non-ASCII character '\xb7' of xlsm file

I am trying to run vba_extract.py in Visual Studio by IronPython. The compilation returns an error:
An unhandled exception of type 'Microsoft.Scripting.SyntaxErrorException' occurred in IronPython.dll
Additional information: Non-ASCII character '\xb7' in file ../../python/vba_extract.py ../../../tests/test.xlsm on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
I searched this issue, and added encoding in Line 2 of vba_extract.py:
#!python
# -*- coding: utf-8 -*-
But it still does not work.
Additionally, I tried the following two lines, which returned the same error:
engine.ExecuteFile(#"../../python/irrelevant.py ../../../tests/test.xlsm");
engine.ExecuteFile(#"../../tests/test.xlsm");
So I guess the problem is from the encoding of test.xlsm.
Could anyone help?

python convert list items to unicode in a single line

This is my code:
mylist=['尺','选择']
[x.encode('utf-8') for x in mylist]
beofore i could even get to the second line of my file, the script returns an error:
SyntaxError: Non-ASCII character '\xe5' in file but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
how do i solve this?
You need to set source code encoding to UTF-8 for this. As per PEP-0263 , Try setting the below line at the top of the script
# -*- coding: utf-8 -*-
The different styles possible for specifying source code encodings are given in the PEP linked above.

Categories

Resources