Replacing from dictionary - Python -


i'm building program able replace characters in message characters user has entered dictionary. of characters given in text file. so, import them, used code:

    d = {}     open("dictionary.txt") d:         line in d:            (key, val) = line.split()            d[str(key)] = val 

it works well, except adds "" start of dictionary. array of to-be-replaced text called 'words'. code have that:

    each in d:            words = ";".join(words)            words = words.replace(d[each],each)            words = words.split(";")            print words 

when hit f5, however, load of gobbledook. here's example: \xef\xbb\xbf\xef\xbb\xbfa+/084&

i'm newbie @ python, appreciated.

ensure save file in dictionnary file in utf-8. notepad++ (windows) there conversion functions if former file not utf-8.

the "" pattern related latin-1 encoding (you won't have if use utf-8 encoding)

then, instead of str(key), use key.encode("utf-8") avoid possible other errors in future.

if want know more, can take python documentation : http://docs.python.org/2/howto/unicode.html


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -