character encoding - Convert a text file from UTF-8 to ASCII to avoid python UnicodeEncodeError? -
i'm getting encoding error script, follows:
from django.template import loader, context t = loader.get_template(filename) c = context({'menus': menus}) print t.render(c) file "../django_to_html.py", line 45, in <module> print t.render(c) unicodeencodeerror: 'ascii' codec can't encode character u'\u2019' in position 34935: ordinal not in range(128)
i don't own script, don't have ability edit it. thing can change filename
supplied doesn't contain unicode character script objecting.
this file text file i'm editing in textmate. can identify , rid of character script barfing on?
could use iconv, , if how?
thanks!
how find nasties in file:
import unicodedata ucd import sys open(sys.argv[1]) f: linex, line in enumerate(f): uline = line.decode('utf-8') bad_line = false charx, char in enumerate(uline): if char <= u'\xff': continue print "line %d, column %d: %s" % ( linex+1, charx+1, ucd.name(char, '<unknown>')) bad_line = true if bad_line: print repr(uline) print
sample output:
line 1, column 6: right single quotation mark line 1, column 10: single low-9 quotation mark u'yadda\u2019foo\u201abar\r\n' line 2, column 4: ideographic space u'fat\u3000space\r\n'
Comments
Post a Comment