UBUNTU Unicode解码错误:'utf-8'编解码器无法解码位置955中的字节0xb7:无效起始字节

wtlkbnrh  于 2023-03-07  发布在  其他
关注(0)|答案(1)|浏览(218)

最近我把我的windows10升级到了Ubuntu,我带来的脚本不起作用了。虽然它们在windows上运行得很好。现在当我试着运行它们时,我得到了一个utf-8编解码器错误。
我安装了venvs,pip和所需的模块(pip list),因为我想也许它缺乏一个utf或unicode模块,但这并没有解决它。
下面是这个文件的代码,它是一个词汇表,可以自动抓取输入单词的结果,虽然还远未完成,但是可以在windows上启动并运行良好(没有终端错误)

from tkinter import *
import csv
import tkinter
import re
from urllib.request import urlopen
from bs4 import BeautifulSoup
import requests

def FkinIndex(number):
    if number >> 1:
        print("NUMBER:::", number)
    elif number == 0:
        number += 1
        return number

# -------------------------------------------------- Function: strVarSet
def strVarSet(keys, values):
    y = 0
    vars = {}
    l = []
    for item in values:
        x = eval(item)
        for xtem in x:
            y = y + 1
            vars[y] = x[xtem]
    print("\nstrVarSet()\nReturning\n", vars, "\n\n", keys)
    return vars, keys

# -------------------------------------------------- Function: DictExtract
def dictExtract():
    with open("dir/dess.txt", "r") as y:
        x = y.readlines()
        d = str(x)
        Dict = eval(d)
    print(Dict)
    print(type(Dict))
    l = []
    for item in Dict:
        print("first print -----\n", item)
        x = eval(item)
        for key in x:
            l.append(key)
    print("\ndictExtract()\nreturning\n", l, "\n", Dict)
    return l, Dict

# -------------------------------------------------- Function: Next
def Next(index, ):
    index = index+1
    print(f"-------------\n{index}\n-------------")
    l, keys = dictExtract()
    vars, momo = strVarSet(l, keys)
    count = 0 # key for dict
    descs = {} # dict
    for f in vars[index]: # for values in DictList[index]
        count = count + 1 # key for dict
        descs[count] = f # Extracting descriptions for Labels
        print("ff\n\n", f)
    return index

# -------------------------------------------------- Function: Current
def Current(index):
    print(f"-------------\n{index}\n-------------")
    l, keys = dictExtract()
    vars, momo = strVarSet(l, keys)
    count = 0  # key for dict
    descs = {}  # dict
    for f in vars[index]:  # for values in DictList[index]
        count = count + 1  # key for dict
        descs[count] = f  # Extracting descriptions for Labels
        print("ff\n\n", f)
    window.update()
    return index, vars, descs

# -------------------------------------------------- Function: Previous
def Previous(index, ):
    index = index-1
    l, keys = dictExtract()
    vars, momo = strVarSet(l, keys)
    count = 0
    descs = {}
    for f in vars[index]:
        count = count + 1
        descs[count] = f
        print(f)


# -------------------------------------------------- Function: DictSaver
def dictSaver(d):
    with open("dir/dess.txt", "a") as y:
        #y = csv.writer(y)
        d = str(d)
        y.write(d + "\n")
        d = {}

# -------------------------------------------------- DictFormer
def DictFormer(l, name):
    d = {name:l}
    print(d)
    dictSaver(d)

# -------------------------------------------------- Function: Button_Words
def Button_Words(words):
    l = []
    for word in words:
        keys, dictList = dictExtract()
        if word not in keys:
            print(word)
            with urlopen(f"https://www.dictionary.com/browse/{word}") as token:
                bsobj = BeautifulSoup(token, "html.parser")
                section = bsobj.find("div", {"class": "css-69s207 e1hk9ate3"})
                l.append(section.get_text())
                for span in section.find_next_sibling("div"):
                    l.append(span.get_text())
            print(l)
            x = DictFormer(l, word)
            l = []
        else:
            continue
# --------------------------------------------------

token1 = urlopen("https://www.dictionary.com/")
token = requests.Request("https://www.dictionary.com/", headers={'User-Agent': 'Mozilla/5.0'})
bsobj = BeautifulSoup(token1, "html.parser")

table = bsobj.find("section", {"class": "g6v6DANjsJKOolEk5qVH"})
step = table.find("span", {"class": re.compile(".*")})
xstep = step.find("a", {"data-linkid": "nx1fkx"})
print(xstep.get_text())

with open("dir/word_list.csv") as word_list:
    word_list = csv.reader(word_list)
    count1 = 0
    for row in word_list:
        count1 = count1 + 1
        row = str(row).strip("[").strip("]").strip("\'")
        print(row)

v1 = "Hello World"
def WordInput(x):
    f = []
    f.append(x)
    with open("dir/word_list.csv", "a") as y:
        writer = csv.writer(y)
        writer.writerow(f)
        y.close()
def wordUnload():
    x=[]
    with open("dir/word_list.csv", "r") as y:
        reader = csv.reader(y)
        for read in reader:
            f = str(read)
            f = f.strip("[").strip("]").strip("\'")
            print(f)

            x.append(f)
    print(x)
    return x

indexxternal = FkinIndex(0)

unloadedw = wordUnload()

window = Tk()
# -------------------------------------------------- Button: Add New Word
NewWordButton = Button(window, text="+", command=lambda: WordInput(input("Add new Word\n> ")))
NewWordButton.grid(row=0, column=1)
# -------------------------------------------------- Button: Load Words
WordsButton = Button(window, text="Words", command=lambda: Button_Words(unloadedw))
WordsButton.grid(row=0, column=0)
# -------------------------------------------------- Button: Next
NextButton = Button(window, text=">", command=lambda: indexxternal==Next(indexxternal))
NextButton.grid(row=0, column=99)
# -------------------------------------------------- Button: Previous
PreviousButton = Button(window, text="<", command=lambda: indexxternal==Previous(indexxternal))
PreviousButton.grid(row=0, column=2)
# -------------------------------------------------- Initial Extraction
l, keys = dictExtract()
count = 0
index, vars, descs = Current(indexxternal)
for f in vars[index]:
    count = count + 1
    descs[count] = f
    print(f)
# -------------------------------------------------- WORD Label

bar1 = tkinter.StringVar(window, str(l[0]).capitalize())
wrd1 = Label(window, textvariable=bar1, font="helvetica 11 underline")
wrd1.grid(row=0, column=3)
try: # --------------------------------------------- Description Label 1
    var1 = tkinter.StringVar(window, str(descs[1]))
    dsc1 = Label(window, textvariable=var1, font="Helvetica 9 italic")
    dsc1.grid(row=1, column=3, pady=5, sticky="W")
except KeyError:
    print("Description out of index in Label 1")
try: # --------------------------------------------- Description Label 2
    var2 = tkinter.StringVar(window, str(descs[2]))
    dsc2 = Label(window, textvariable=var2)
    dsc2.grid(row=2, column=3, pady=1, sticky="W")
except KeyError:
    print("Description out of index in Label 2")
try: # --------------------------------------------- Description Label 3
    var3 = tkinter.StringVar(window, str(descs[3]))
    dsc3 = Label(window, textvariable=var3)
    dsc3.grid(row=3, column=3,pady=1, sticky="W")
except KeyError:
    print("Description out of index in Label 3")
try: # --------------------------------------------- Description Label 4
    var4 = tkinter.StringVar(window, str(descs[4]))
    dsc4 = Label(window, textvariable=var4)
    dsc4.grid(row=4, column=3,pady=1, sticky="W")
except KeyError:
    print("Description out of index in Label 4")
try: # --------------------------------------------- Description Label 5
    var5 = tkinter.StringVar(window, str(descs[5]))
    dsc5 = Label(window, textvariable=var5)
    dsc5.grid(row=5, column=3,pady=1, sticky="W")
except KeyError:
    print("Description out of index in Label 5")
try: # --------------------------------------------- Description Label 6
    var6 = tkinter.StringVar(window, str(descs[6]))
    dsc6 = Label(window, textvariable=var6)
    dsc6.grid(row=6, column=3,pady=1, sticky="W")
except KeyError:
    print("Description out of index in Label 6")
try: # --------------------------------------------- Description Label 7
    var7 = tkinter.StringVar(window, str(descs[7]))
    dsc7 = Label(window, textvariable=var7)
    dsc7.grid(row=7, column=3,pady=1, sticky="W")
except KeyError:
    print("Description out of index in Label 7")

try: # --------------------------------------------- Description Label 8
    var8 = tkinter.StringVar(window, str(descs[8]))
    dsc8 = Label(window, textvariable=var8)
    dsc8.grid(row=8, column=3, sticky="W")
except KeyError:
    print("Description out of index in Label 8")
try: # --------------------------------------------- Description Label 9
    var9 = tkinter.StringVar(window, str(descs[9]))
    dsc9 = Label(window, textvariable=var9)
    dsc9.grid(row=9, column=3, sticky="W")
except KeyError:
    print("Description out of index in Label 9")
try: # --------------------------------------------- Description Label 10
    var10 = tkinter.StringVar(window, str(descs[10]))
    dsc10 = Label(window, textvariable=var10)
    dsc10.grid(row=10, column=3, sticky="W")
except KeyError:
    print("Description out of index in Label 10")

loadword1 = tkinter.StringVar(window)

window.mainloop()

下面是我尝试运行它时得到的错误:Error.有人知道解决这个问题的方法吗?

vfwfrxfs

vfwfrxfs1#

解决!问题是dess.txt的编码。Windows对其txt文件进行不同的编码,这会导致unicode错误。
对于任何面临类似问题的人:

  1. cd到你的txt的目录
    1.通过终端输入gedit name.txt,在GEdit中打开你的文本文件(你可以在ubuntu软件应用程序上获得它
    1.点击保存,在窗口底部更改文件的编码并覆盖它。
    非常感谢@snakecharmerb -我真的被迷住了。

相关问题