這是一個將隨機生成的句子發送到不和諧聊天的腳本。但它偶爾會遇到錯誤:UnicodeDecodeError: 'ascii' codec cant decode byte 0xef in position 2141: ordinal not in range(128)
UnicodeDecodeError:'ascii'編解碼器無法解碼位置2141中的字節0xef:序號不在範圍內(128)
我該如何解決這個錯誤?
代碼:
import asyncio
import random
import discord.ext.commands
import markovify
import nltk
import re
with open("/root/sample.txt") as f:
text = f.read()
class POSifiedText(markovify.Text):
def word_split(self, sentence):
words = re.split(self.word_split_pattern, sentence)
words = [w for w in words if len(w) > 0]
words = [" :: ".join(tag) for tag in nltk.pos_tag(words)]
return words
def word_join(self, words):
sentence = "".join(word.split("::")[0] for word in words)
return sentence
text_model = POSifiedText(text, state_size=1)
client = discord.Client()
async def background_loop():
await client.wait_until_ready()
while not client.is_closed:
channel = client.get_channel('286342556600762369')
messages = [(text_model.make_sentence(tries=33, max_overlap_total=10, default_max_overlap_ratio=0.5))]
await client.send_message(channel, random.choice(messages))
await asyncio.sleep(15)
client.loop.create_task(background_loop())
client.run("MjY2NjkwNDY4MjI4NzU5NTU4.C5jcdw.WFfBTUmAY7UcrwKTwYFJ9_bFHjI")
的錯誤發生在線路9.
您的示例包含非ascii字符。在open()調用中,指定正確的編碼。 – alexis
重複[UnicodeDecodeError:'charmap'編解碼器無法解碼位置Y中的字節X:字符映射到](http://stackoverflow.com/questions/9233027/unicodedecodeerror-charmap-codec-cant-decode-byte- x-in-position-y-character) –
alexis
但是你的問題與nltk無關,或者與你正在構建的chatbot無關。停止標記一切'nltk';縮小您的問題範圍,Google解決方案很容易。 – alexis