#!/usr/bin/env python # -*- coding: utf-8 -*- # The comment up above lets me type utf-8 characters in this file. # ------- # See # * wikipedia # character encodings # utf-8 # * articles & python docs # http://docs.python.org/tut/node5.html # http://docs.python.org/ref/strings.html # http://www.jorendorff.com/articles/unicode/python.html # * google "python unicode" or "python utf-8" # * list of characters # http://www.utf8-chartable.de/ # What you need to know : # (1) Put the coding comment at the top of the file. # (2) Type strings or chars as u"..." # (3) Type funky characters as # - the funky char itself, pasted from a special editor # - \uxxxx where xxxx is a 16-bit hex value # - \Uxxxxxxxx where xxxxxxxx is a 32-bit hex value # - \N{name} for the character with that name, for example # RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK # (4) Open files with codecs.open # (5) Convert text from one code to another with text.encode('codec-name') # -------- import codecs # This is a unicode string. text = u" Ándre says (α, β, γ, δ )" print "text:", text print "text as utf-8:", text.encode('utf-8') # print "text as latin-1:", text.encode('latin-1') # gives an error # This opens a file for reading/writing in utf-8 encoding. filename = 'from_python.txt' print "writing text to", filename file = codecs.open(filename, 'w', 'utf-8') file.write(text) file.close()