gpt4 book ai didi

python - Unicode解码错误: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

转载 作者:行者123 更新时间:2023-11-30 23:22:56 27 4
gpt4 key购买 nike

我有这个代码:

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

当我运行代码时一切正常。然后我插入文本并按下按钮。但后来我得到了这个错误:

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)

如何解决这个问题?起初,当我尝试运行代码时,我收到了相同的错误消息,然后我添加了 lambda:,现在当我单击应用程序中的按钮时,问题就会出现。

最佳答案

您必须将字符串解码为 utf-8(或其他编码),然后将 unicode 字符串替换为其他字符串。这段代码实现了您想要实现的目标:

paragraph = paragrah.decode('utf-8').replace(u'\u014c\u0106\u014d','-')
# '\u014c\u0106\u014d' is the unicode representation of characters ŌĆō

关于python - Unicode解码错误: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24218298/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com