Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of ascii encoding error in python

Tags:

python

string = "Deepika Padukone, Esha Gupta or Yami Gautam - Who's looks hotter and sexier? Vote! - It's ... Deepika Padukone, Esha Gupta or Yami Gautam…. Deepika Padukone, Esha Gupta or Yami Gautam ... Tag: Deepika Padukone, Esha Gupta, Kalki Koechlin, Rang De Basanti, Soha Ali Khan, Yami  ... Amitabh Bachchan and Deepika Padukone to be seen in Shoojit Sircar's Piku ..."

fp = open("test.txt", "w+");

fp.write("%s" %string);

after running the above code I have got the following error.

File "encode_error.py", line 1

SyntaxError: Non-ASCII character '\xe2' in file encode_error.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
like image 903
user3770743 Avatar asked Jun 24 '14 10:06

user3770743


1 Answers

You have a U+2026 HORIZONTAL ELLIPSIS character in your string definition:

... Deepika Padukone, Esha Gupta or Yami Gautam…. ...
                                               ^

Python requires that you declare the source code encoding if you are to use any non-ASCII characters in your source.

Your options are to:

  • Declare the encoding, as specified in the linked PEP 263. It's is a comment that must be the first or second line of your source file.

    What you set it to depends on your code editor. If you are saving files encoded as UTF-8, then the comment looks something like:

    # coding: utf-8
    

    but the format is flexible. You can spell it encoding too, for example, and use = instead of :.

  • Replace the horizontal ellipsis with three dots, as used in the rest of the string

  • Replace the codepoint with \xhh escape sequences to represent encoded data. U+2026 encoded to UTF-8 is \xe2\x80\xa6.
like image 56
Martijn Pieters Avatar answered Oct 17 '22 02:10

Martijn Pieters