我有以下代码:
# -*- 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 = IntoSentences(text.get(1.0,END)))
button.pack()
root.mainloop()
当我尝试运行此代码时,出现以下错误:
C:\Users\Indrek>C:\Python27\Myprojects\caps_main.py
Traceback (most recent call last):
File "C:\Python27\Myprojects\caps_main.py", line 25, in <module>
button=Button(root, text ="Create text with caps.", command = IntoSentences(
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)
这是什么问题?我对这个问题做了一些研究,但我读的帖子对我没有帮助。我应该在具体代码中更改什么?
答案 0 :(得分:0)
我的错误是使用命令错误,就像Bryan Oakley说的那样。现在我的代码正在关注,一切正常:
# -*- 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
def new_sentences(sentenceList):
for i in sentenceList:
import re
from random import randint
s6nade_arv=len(lause.split(' '))
while True:
asendatava_idx=randint(0,s6nade_arv-1)
wordList = re.sub("[^\w]", " ", lause).split()
asendatav_s6na=wordList[asendatava_idx]
if asendatav_s6na.lower() not in forbiddenWords:
break
uus_lause=lause.replace(asendatav_s6na, "______")
new_sentences.append(uus_lause)
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()
我改变的是我将lambda:添加到button=Button(root, text ="Create text with caps.", command =lambda: IntoSentences(text.get(1.0,END)))