Skip to content

Instantly share code, notes, and snippets.

@zhasm
Created March 5, 2012 00:33
Show Gist options
  • Save zhasm/1975646 to your computer and use it in GitHub Desktop.
Save zhasm/1975646 to your computer and use it in GitHub Desktop.
display encoding
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
def encode(c):
"""show the other repr of unicode str c"""
ens=['utf-8', 'gbk', 'big5']
print "Unicode:\t%s" % (repr(c))
for en in ens:
try:
print "%s:\t%s" % (en, repr(c.encode(en)))
except Exception, e:
print "Error for %s: %s" % (en, str(e))
def getInput():
c=' '.join(sys.argv[1:])
c=unicode(c, encoding='utf-8', errors='replace')
return c
def usage():
print "Usage: %s <text_to_decode>" % sys.argv[0]
def main():
c=getInput()
if not c:
usage()
exit()
encode(getInput())
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment