fork download
  1. if __name__ == '__main__':
  2. # define our Unicode string
  3. uni = u"SSM1620 Jul15-defect Id 347603 - empty check added for avoid exception in case we get empty string �pooja khandelwal -6/15/2015"
  4.  
  5. # UTF-8 and UTF-16 can fully encode *any* unicode string
  6.  
  7. print "UTF-8", repr(uni.encode('utf-8'))
  8. print "UTF-16", repr(uni.encode('utf-16'))
  9.  
  10. # ASCII can only work with code values from 0-127. Below we tell Python
  11.  
  12. print "ASCII ", uni.encode('ascii','replace')
  13.  
  14. # ISO-8859-1 is similar to ASCII
  15.  
  16. print "ISO-8859-1 ", uni.encode('iso-8859-1','replace')
  17.  
  18. uni = uni.encode('utf-8')
  19. bstr = unicode(uni, 'utf-8')
  20. print "Back from UTF-8:", repr(bstr)
  21.  
Success #stdin #stdout 0.01s 7268KB
stdin
Standard input is empty
stdout
UTF-8 'SSM1620 Jul15-defect Id 347603 - empty check added for avoid exception in case we get empty string \xc3\xaf\xc2\xbf\xc2\xbdpooja khandelwal -6/15/2015'
UTF-16 '\xff\xfeS\x00S\x00M\x001\x006\x002\x000\x00 \x00J\x00u\x00l\x001\x005\x00-\x00d\x00e\x00f\x00e\x00c\x00t\x00 \x00I\x00d\x00 \x003\x004\x007\x006\x000\x003\x00 \x00-\x00 \x00e\x00m\x00p\x00t\x00y\x00 \x00c\x00h\x00e\x00c\x00k\x00 \x00a\x00d\x00d\x00e\x00d\x00 \x00f\x00o\x00r\x00 \x00a\x00v\x00o\x00i\x00d\x00 \x00e\x00x\x00c\x00e\x00p\x00t\x00i\x00o\x00n\x00 \x00i\x00n\x00 \x00c\x00a\x00s\x00e\x00 \x00w\x00e\x00 \x00g\x00e\x00t\x00 \x00e\x00m\x00p\x00t\x00y\x00 \x00s\x00t\x00r\x00i\x00n\x00g\x00 \x00\xef\x00\xbf\x00\xbd\x00p\x00o\x00o\x00j\x00a\x00 \x00k\x00h\x00a\x00n\x00d\x00e\x00l\x00w\x00a\x00l\x00 \x00-\x006\x00/\x001\x005\x00/\x002\x000\x001\x005\x00'
ASCII  SSM1620 Jul15-defect Id 347603 - empty check added for avoid exception in case we get empty string ???pooja khandelwal -6/15/2015
ISO-8859-1  SSM1620 Jul15-defect Id 347603 - empty check added for avoid exception in case we get empty string �pooja khandelwal -6/15/2015
Back from UTF-8: u'SSM1620 Jul15-defect Id 347603 - empty check added for avoid exception in case we get empty string \xef\xbf\xbdpooja khandelwal -6/15/2015'