Issue
I want to replace a long dash (–
) with a short dash (-
). My code:
if " – " in string:
string = string.replace(" – ", " - ")
results in the following error:
SyntaxError: Non-ASCII character '\xe2' in file ./script.py on line 76, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
How can I fix this?
Solution
Long dash is not an ASCII character. Declare encoding of your script, like this (somewhere on top):
#-*- coding: utf-8 -*-
There are also other encodings beside utf-8
but it is always safe to use utf-8
if not working with ASCII characters which covers virtually all (unicode) characters.
See PEP 0263 for more info.
Answered By - Santosh Kumar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.