import re,sys, urllib, os Names = ['"', ''', '&', '<', '>', ' ', '¡', '¤', '¢', '£', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '™', '¯', '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿', '×', '÷', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ', 'Œ', 'œ', 'Š', 'š', 'Ÿ', 'ˆ', '˜', ' ', ' ', ' ', '‌', '‍', '‎', '‏', '–', '—', '‘', '’', '‚', '“', '”', '„', '†', '‡', '…', '‰', '‹', '›', '€'] Numbers = ['"', ''', '&', '<', '>', ' ', '¡', '¤', '¢', '£', '¥', '¦', '§', '¨', '©', 'ª', '«', '¬', '­', '®', '™', '¯', '°', '±', '²', '³', '´', 'µ', '¶', '·', '¸', '¹', 'º', '»', '¼', '½', '¾', '¿', '×', '÷', 'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß', 'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', 'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ', 'Œ', 'œ', 'Š', 'š', 'Ÿ', 'ˆ', '˜', ' ', ' ', ' ', '‌', '‍', '‎', '‏', '–', '—', '‘', '’', '‚', '“', '”', '„', '†', '‡', '…', '‰', '‹', '›', '€'] def ConvertTypes(string, convertTo='Names'): for names_with, numbers_with in zip(Names, Numbers): if convertTo == 'Names': if numbers_with in string: string = string.replace(numbers_with,names_with.replace(' ','')) elif convertTo == 'Numbers': if names_with in string: string = string.replace(names_with,numbers_with.replace(' ','')) else: return 'Dumbass! Follow the options!' return string def ConvertNumCodes(string): for i in re.compile(r"&#[0-9]{1,4};").findall(string): string = string.replace(i,'%c'%(int(i.replace('&#','').replace(';','')))) return string # Test runs # ========= toNums = 'Ò, Ó, Õ, Ø, Í, È' toNames = 'Ò, Ó, Õ, Ø, Í, È' print '================================================================' print '' print 'Convert from alpha codes to numeric codes:' print '------------------------------------------\n' print 'from: ' + toNums Converted = ConvertTypes(toNums,convertTo='Numbers') print 'To: ' + Converted print '' print '================================================================' print '' print 'Convert from numeric codes to alpha codes:' print '------------------------------------------\n' print 'From: ' + toNames Converted = ConvertTypes(toNames,convertTo='Names') print 'To: ' + Converted print '' print '================================================================' print '' print 'Convert numeric codes to characters:' print '------------------------------------\n' print 'From: ' + toNames Characters = ConvertNumCodes(toNames) print 'To: ' + Characters print '' print '================================================================'