gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:21:17 25 4
gpt4 key购买 nike

我正在将从 jobs API 获取的数据写入 Google 电子表格。 'latin-1' 的编码直到第 93 页,但当达到第 94 页时,就会出现异常。我使用了不同的以下技术,但“latin-1”执行了最大分页。其他已被评论(因为它们在第 #65 页上消失)。您能否告诉我如何修改未注释的(即 .encode('latin-1'))以将 199 页安全地写入电子表格上?代码如下:提前感谢这方面的任何指导方针。

  def append_data(self,worksheet,row,start_row, start_col,end_col):
r = start_row #last_empty_row(worksheet)
j = 0
i = start_col
while (i <= end_col):
try:
worksheet.update_cell(r,i,unicode(row[j]).encode('latin-1','ignore'))
#worksheet.update_cell(r,i,unicode(row[j]).decode('latin-1').encode("utf-
16"))
#worksheet.update_cell(r,i,unicode(row[j]).encode('iso-8859-1'))
#worksheet.update_cell(r,i,unicode(row[j]).encode('latin-1').decode("utf-
8"))
#worksheet.update_cell(r,i,unicode(row[j]).decode('utf-8'))
#worksheet.update_cell(r,i,unicode(row[j]).encode('latin-1', 'replace'))
#worksheet.update_cell(r,i,unicode(row[j]).encode(sys.stdout.encoding,
'replace'))
#worksheet.update_cell(r,i,row[j].encode('utf8'))
#worksheet.update_cell(r,i,filter(self.onlyascii(str(row[j]))))

except Exception as e:
self.ehandling_obj.error_handler(self.ehandling_obj.SPREADSHEET_ERROR,[1])
try:
worksheet.update_cell(r,i,'N/A')
except Exception as ee:
y = 23
j = j + 1
i = i + 1

最佳答案

您正在对字节字符串值调用 unicode(),这意味着 Python 必须首先解码为 Unicode:

>>> unicode('\xea')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xea in position 0: ordinal not in range(128)

正是这种解码失败,而不是从 Unicode 编码回字节字符串。

您要么已经有 Latin-1 输入数据,要么应该使用适当的编解码器进行解码:

unicode(row[j], 'utf8').encode('latin1')

或使用str.decode():

row[j].decode('utf8').encode('latin1')

我在这里选择 UTF-8 作为示例,您没有提供有关输入数据或其可能编码的任何详细信息。您需要在这里自己选择正确的编解码器。

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

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