for encoding in ('utf-8', 'cp1252'):
    out = codecs.open('%s.txt' % encoding, 'w', encoding)
out.write('# coding = %s\n' % encoding)
out.write('\u201chello se\xf1nor\u201d')
out.close()
Read a unicode file in python which declares its encoding in the same way as python source