Friday, 15 July 2011

How to get rid of ascii encoding error in python -



How to get rid of ascii encoding error in python -

string = "deepika padukone, esha gupta or yami gautam - who's looks hotter , 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 , deepika padukone seen in shoojit sircar's piku ..." fp = open("test.txt", "w+"); fp.write("%s" %string);

after running above code have got next error.

file "encode_error.py", line 1 syntaxerror: non-ascii character '\xe2' in file encode_error.py on line 1, no encoding declared; see http://www.python.org/peps/pep-0263.html details

you have u+2026 horizontal ellipsis character in string definition:

... deepika padukone, esha gupta or yami gautam…. ... ^

python requires declare source code encoding if utilize non-ascii characters in source.

your options to:

declare encoding, specified in linked pep 263. it's comment must first or sec line of source file.

what set depends on code editor. if saving files encoded utf-8, comment looks like:

# coding: utf-8

but format flexible. can spell encoding too, example, , utilize = instead of :.

replace horizontal ellipsis 3 dots, used in rest of string

replace codepoint \xhh escape sequences represent encoded data. u+2026 encoded utf-8 \xe2\x80\xa6.

python

No comments:

Post a Comment