How do I declare an encoding for Python source code? - python

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.

Add
#!/usr/bin/env python
# -*- coding: utf-8 -*-
On the first 2 lines.

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!'

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

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)

Encode Strings in Python

I want to run my code on terminal but it shows me this error :
SyntaxError: Non-ASCII character '\xd8' in file streaming.py on line
72, but no encoding declared; see http://python.org/dev/peps/pep-0263/
for detail
I tried to encode the Arabic string using this :
# -*- coding: utf-8 -*-
st = 'المملكة العربية السعودية'.encode('utf-8')
It's very important for me to run it on the terminal so I can't use IDLE.
The problem is since you are directly pasting your characters in to a python file, the interpreter (Python 2) attempts to read them as ASCII (even before you encode, it needs to define the literal), which is illegal. What you want is a unicode literal if pasting non-ASCII bytes:
x=u'المملكة العربية السعودية' #Or whatever the corresponding bytes are
print x.encode('utf-8')
You can also try to set the entire source file to be read as utf-8:
#/usr/bin/python
# -*- coding: utf-8 -*-
and don't forget to make it run-able, and lastly, you can import the future from Python 3:
from __future__ import unicode_literal
at the top of the file, so string literals by default are utf-8. Note that \xd8 appears as phi in my terminal, so make sure the encoding is correct.

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.

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

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

Python: Can't write to file - UnicodeEncodeError

This code should write some text to file.
When I'm trying to write my text to console, everything works. But when I try to write the text into the file, I get UnicodeEncodeError. I know, that this is a common problem which can be solved using proper decode or encode, but I tried it and still getting the same UnicodeEncodeError. What am I doing wrong?
I've attached an example.
print "(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)".decode("utf-8")%(dict.get('name'),dict.get('description'),dict.get('ico'),dict.get('city'),dict.get('ulCislo'),dict.get('psc'),dict.get('weby'),dict.get('telefony'),dict.get('mobily'),dict.get('faxy'),dict.get('emaily'),dict.get('dic'),dict.get('ic_dph'),dict.get('kategorie')[0],dict.get('kategorie')[1],dict.get('kategorie')[2])
(StarBuy s.r.o.,Inzertujte s foto, auto-moto, oblečenie, reality, prácu, zvieratá, starožitnosti, dovolenky, nábytok, všetko pre deti, obuv, stroj....
with open("test.txt","wb") as f:
f.write("(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)".decode("utf-8")%(dict.get('name'),dict.get('description'),dict.get('ico'),dict.get('city'),dict.get('ulCislo'),dict.get('psc'),dict.get('weby'),dict.get('telefony'),dict.get('mobily'),dict.get('faxy'),dict.get('emaily'),dict.get('dic'),dict.get('ic_dph'),dict.get('kategorie')[0],dict.get('kategorie')[1],dict.get('kategorie')[2]))
UnicodeEncodeError: 'ascii' codec can't encode character u'\u010d' in position 50: ordinal not in range(128)
Where could be the problem?
To write Unicode text to a file, you could use io.open() function:
#!/usr/bin/env python
from io import open
with open('utf8.txt', 'w', encoding='utf-8') as file:
file.write(u'\u010d')
It is default on Python 3.
Note: you should not use the binary file mode ('b') if you want to write text.
# coding: utf8 that defines the source code encoding has nothing to do with it.
If you see sys.setdefaultencoding() outside of site.py or Python tests; assume the code is broken.
#ned-batchelder is right. You have to declare that the system default encoding is "utf-8". The coding comment # -*- coding: utf-8 -*- doesn't do this.
To declare the system default encoding, you have to import the module sys, and call sys.setdefaultencoding('utf-8'). However, sys was previously imported by the system and its setdefaultencoding method was removed. So you have to reload it before you call the method.
So, you will need to add the following codes at the beginning:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
You may need to explicitly declare that python use UTF-8 encoding.
The answer to this SO question explains how to do that: Declaring Encoding in Python
For Python 2:
Declare document encoding on top of the file (if not done yet):
# -*- coding: utf-8 -*-
Replace .decode with .encode:
with open("test.txt","wb") as f:
f.write("(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)".encode("utf-8")%(dict.get('name'),dict.get('description'),dict.get('ico'),dict.get('city'),dict.get('ulCislo'),dict.get('psc'),dict.get('weby'),dict.get('telefony'),dict.get('mobily'),dict.get('faxy'),dict.get('emaily'),dict.get('dic'),dict.get('ic_dph'),dict.get('kategorie')[0],dict.get('kategorie')[1],dict.get('kategorie')[2]))

Categories

Resources