Python 2.7: How to print this character? - python

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)

Related

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

How do I declare an encoding for Python source code?

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

Using urlencode for Devanagari text

The following code:
import simplejson,urllib,urllib2
query=[u'नेपाल']
urlbase="http://search.twitter.com/search.json"
values={'q':query[0]}
data=urllib.urlencode(values)
req=urllib2.Request(urlbase,data)
response=urllib2.urlopen(req)
json=simplejson.load(response)
print json
throws exception:
SyntaxError: Non-ASCII character '\xe0' in file ques.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
The code works if query contains standard ASCII characters. I tried looking at the suggested link but couldn't figure out how to specify encoding for Devanagari characters.
You need to add the UTF-8 header to your file to tell the Python interpreter that there are unicode literals. You also have to encode the parameters as UTF-8. Here's a working version:
# -*- coding: utf-8 -*-
import simplejson,urllib,urllib2
query=[u'नेपाल']
urlbase="http://search.twitter.com/search.json"
values={'q':query[0].encode('utf-8')}
data=urllib.urlencode(values)
req=urllib2.Request(urlbase,data)
response=urllib2.urlopen(req)
json=simplejson.load(response)
print json

Categories

Resources