Reputation: 143
I have this code:
# -*- coding: utf-8 -*-
forbiddenWords=['for', 'and', 'nor', 'but', 'or', 'yet', 'so', 'not', 'a', 'the', 'an', 'of', 'in', 'to', 'for', 'with', 'on', 'at', 'from', 'by', 'about', 'as']
def IntoSentences(paragraph):
paragraph = paragraph.replace("–", "-")
import nltk.data
sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')
sentenceList = sent_detector.tokenize(paragraph.strip())
return sentenceList
from Tkinter import *
root = Tk()
var = StringVar()
label = Label( root, textvariable=var)
var.set("Fill in the caps: ")
label.pack()
text = Text(root)
text.pack()
button=Button(root, text ="Create text with caps.", command =lambda: IntoSentences(text.get(1.0,END)))
button.pack()
root.mainloop()
Everything works fine when I run the code. Then I insert the text and press the button. But then I get this error:
C:\Users\Indrek>caps_main.py
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
File "C:\Python27\Myprojects\caps_main.py", line 25, in <lambda>
button=Button(root, text ="Create text with caps.", command =lambda: IntoSen
tences(text.get(1.0,END)))
File "C:\Python27\Myprojects\caps_main.py", line 7, in IntoSentences
paragraph = paragraph.replace("ŌĆō", "-")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal
not in range(128)
How to fix this problem? At first I had the same error message, when I tried to run thecode, then I added the lambda: and now the problem appears when i click the button in my app.
Upvotes: 1
Views: 6256
Reputation: 726
You'll have to decode the string to utf-8(or some other encoding) and then replace the unicode string to someting else. This piece of code does what you are trying to achieve:
paragraph = paragrah.decode('utf-8').replace(u'\u014c\u0106\u014d','-')
# '\u014c\u0106\u014d' is the unicode representation of characters ŌĆō
Upvotes: 3