简体   繁体   English

Python UnicodeDecodeError:'ascii'编解码器无法解码位置0的字节0xe2:序数不在范围内(128)

[英]Python UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

I have following 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 = IntoSentences(text.get(1.0,END)))
button.pack()

root.mainloop()

When I try to run this code, I get following error: 当我尝试运行此代码时,出现以下错误:

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)

What is the problem here? 这里有什么问题? I made some research about this problem, but posts i read were not helpful for me. 我对此问题进行了一些研究,但我阅读的帖子对我没有帮助。 What should I change in my specific code? 我应该更改我的特定代码吗?

My mistake was using command wrong, just like Bryan Oakley said. 我的错误是使用命令错误,就像布莱恩·奥克利(Bryan Oakley)所说的那样。 Now my code is following and everything works: 现在我的代码如下,一切正常:

# -*- 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()

What I changed is I added lambda: to button=Button(root, text ="Create text with caps.", command =lambda: IntoSentences(text.get(1.0,END))) 我所更改的是我将lambda:添加到button=Button(root, text ="Create text with caps.", command =lambda: IntoSentences(text.get(1.0,END)))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 3 UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置0中的字节0xe2:序数不在范围内(128) - Python 3 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128) UnicodeDecodeError:&#39;ascii&#39; 编解码器无法解码位置 14 中的字节 0xe2:在 GAE python 中序号不在范围内(128)? - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 14: ordinal not in range(128) in GAE python? UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置0中的字节0xe2:序号不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128) UnicodeDecodeError:“ascii”编解码器无法解码位置 13 中的字节 0xe2:序号不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128) UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置35的字节0xe2:序数不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 35: ordinal not in range(128) UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置4中的字节0xe2:序数不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 4: ordinal not in range(128) 安装熊猫:UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置 72 中的字节 0xe2:序号不在范围内(128) - Installing pandas: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 72: ordinal not in range(128) UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置139中的字节0xe2:序数不在范围内(128) - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 139: ordinal not in range(128) Python UnicodeDecodeError:&#39;ascii&#39;编解码器不能解码不在范围内的字节0xe2序数(128) - Python UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 ordinal not in range(128) UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码位置13的字节0xe2:关于读取文件的序数不在range(128)中 - UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128), regarding reading in files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM