gpt4 book ai didi

python - 为什么我得到 “UnicodeDecodeError: ' utf- 8' codec can' t解码位置227 : invalid start byte” error中的字节0xfd

转载 作者:行者123 更新时间:2023-12-02 10:56:37 26 4
gpt4 key购买 nike

当我收到“UnicodeDecodeError:'utf-8'编解码器无法解码位置227字节0xfd:无效的起始字节”错误时,我正在为我的类(class)做准备,即使我使用的文本文件看起来还可以。
这是我的代码:

with open("Oyuncular.txt","r",encoding="utf-8") as file:
gs_listesi = []
fb_listesi=[]
bjk_listesi=[]
for i in file:
i = i[:-1]
elemanlar = i.split(",")
if(elemanlar[1]=="BJK"):
bjk_listesi.append(elemanlar[0])
elif(elemanlar[1]=="GS"):
gs_listesi.append(elemanlar[0])
else:
fb_listesi.append(elemanlar[0])
with open("BJK_Oyuncular.txt","w",encoding="utf-8") as file2:
for i in bjk_listesi:
file2.write(bjk_listesi[i]+ "\n")
with open("GS_Oyuncular.txt","w",encoding="utf-8") as file3:
for i in gs_listesi:
file3.write(gs_listesi[i]+ "\n")
with open("FB_Oyuncuları.txt","w",encoding="utf-8") as file4:
for i in fb_listesi:
file3.write(gs_listesi[i]+ "\n")

我的文字档:
Fernando Muslera, GS
Victor Ruiz, BJK
Harun Tekin, FB
Okan Kocuk, GS
Loris Karius, BJK
Mauricio Isla, FB
Christian Luyindama, GS
Enzo Roco, BJK
Simon Falette, FB
Jean Michael Sei, GS
Domagoj Vida, BJK
Taylan Antalyalı, GS
Serdar Aziz, FB
Emre Belözoğlu, FB
Mohamed Naser Elsayed Elneny, BJK

我该如何解决?

最佳答案

有几个问题gs_listesi[i]将引发错误,因为i是文本类型。另外,当在ValueError: I/O operation on closed file.中引用file3时,也会收到错误file4。我无法收到您发布的错误,请参见下面的代码。

with open("Oyuncular.txt","r",encoding="utf-8") as file:
gs_listesi = []
fb_listesi=[]
bjk_listesi=[]
for i in file:
i = i[:-1]
elemanlar = i.split(",")
if(elemanlar[1]=="BJK"):
bjk_listesi.append(elemanlar[0])
elif(elemanlar[1]=="GS"):
gs_listesi.append(elemanlar[0])
else:
fb_listesi.append(elemanlar[0])
print(gs_listesi)
print(fb_listesi)
print(bjk_listesi)
with open("C:\\Py\\Stackoverflow\\BJK_Oyuncular.txt","w",encoding="utf-8") as file2:
for i2 in bjk_listesi:
file2.write(i2 + "\n")
with open("C:\\Py\\Stackoverflow\\GS_Oyuncular.txt","w",encoding="utf-8") as file3:
for i3 in gs_listesi:
file3.write(i3 + "\n")
with open("C:\\Py\\Stackoverflow\\FB_Oyuncuları.txt","w",encoding="utf-8") as file4:
for i4 in fb_listesi:
file4.write(i4 + "\n")

关于python - 为什么我得到 “UnicodeDecodeError: ' utf- 8' codec can' t解码位置227 : invalid start byte” error中的字节0xfd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61959237/

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