【问题标题】:"UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80" while loading pickle file using pydrive on google colaboratory“UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80” while loading pickle file using pydrive on google colaboratory
【发布时间】:2018-03-07 06:15:05
【问题描述】:

我是使用 google colaboratory (colab) 和 pydrive 的新手。我正在尝试使用 colab 将数据加载到“CAS_num_strings”中,该数据写入我的谷歌驱动器上特定目录中的泡菜文件中:

pickle.dump(CAS_num_strings,open('CAS_num_strings.p', 'wb'))
dump_meta = {'title': 'CAS.pkl', 'parents': [{'id':'1UEqIADV_tHic1Le0zlT25iYB7T6dBpBj'}]} 
pkl_dump = drive.CreateFile(dump_meta)
pkl_dump.SetContentFile('CAS_num_strings.p')
pkl_dump.Upload()
print(pkl_dump.get('id'))

Where 'id':'1UEqIADV_tHic1Le0zlT25iYB7T6dBpBj' 确保它有一个特定的父文件夹,该文件夹由该 id 给出。最后一个打印命令给了我输出:

'1ZgZfEaKgqGnuBD40CY8zg0MCiqKmi1vH'

因此,我可以创建并转储 ID 为“1ZgZfEaKgqGnuBD40CY8zg0MCiqKmi1vH”的 pickle 文件。现在,为了不同的目的,我想在另一个 colab 脚本中加载这个 pickle 文件。为了加载,我使用命令集:

cas_strings = drive.CreateFile({'id':'1ZgZfEaKgqGnuBD40CY8zg0MCiqKmi1vH'})
print('title: %s, mimeType: %s' % (cas_strings['title'], cas_strings['mimeType']))
print('Downloaded content "{}"'.format(cas_strings.GetContentString()))

这给了我输出:

title: CAS.pkl, mimeType: text/x-pascal

---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-9-a80d9de0fecf> in <module>()
     30 cas_strings = drive.CreateFile({'id':'1ZgZfEaKgqGnuBD40CY8zg0MCiqKmi1vH'})
     31 print('title: %s, mimeType: %s' % (cas_strings['title'], cas_strings['mimeType']))
---> 32 print('Downloaded content "{}"'.format(cas_strings.GetContentString()))
     33 
     34 

/usr/local/lib/python3.6/dist-packages/pydrive/files.py in GetContentString(self, mimetype, encoding, remove_bom)
    192                     self.has_bom == remove_bom:
    193       self.FetchContent(mimetype, remove_bom)
--> 194     return self.content.getvalue().decode(encoding)
    195 
    196   def GetContentFile(self, filename, mimetype=None, remove_bom=False):

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

如您所见,它找到了文件 CAS.pkl 但无法解码数据。我希望能够解决这个错误。我了解正常的 utf-8 编码/解码在正常的泡菜转储和使用“wb”和“rb”选项加载期间工作顺利。但是在目前的情况下,转储后我似乎无法从上一步中创建的谷歌驱动器中的泡菜文件中加载它。错误存在于我无法指定如何在“return self.content.getvalue().decode(encoding)”处解码数据的地方。我似乎无法从这里 (https://developers.google.com/drive/v2/reference/files#resource-representations) 找到要修改的关键字/元数据标签。任何帮助表示赞赏。谢谢

【问题讨论】:

  • 我看到你调用了 pickle.dump,但你从来没有调用过 pickle.load -- 这可能是问题吗?
  • 这是一个很好的观点。我想确切地尝试一下,但无法弄清楚它在序列中的哪个位置可以使用 pydrive。如果不结合使用 pydrive,它就无法工作,因为只有 pydrive 可以从 google drive 的 id 中检索文件

标签: python utf-8 pickle google-colaboratory pydrive


【解决方案1】:

问题是 GetContentString 仅在内容是有效的 UTF-8 字符串 (docs) 时才有效,而您的泡菜不是。

不幸的是,您必须做一些额外的工作,因为没有GetContentBytes——您必须将内容保存到文件中并将它们读回。这是一个工作示例: https://colab.research.google.com/drive/1gmh21OrJL0Dv49z28soYq_YcqKEnaQ1X

【讨论】:

  • 谢谢,我的答案和你的几乎一模一样!
【解决方案2】:

实际上,在朋友的帮助下,我找到了一个优雅的答案。我使用 GetContentFile 而不是 GetContentStrings,它是 SetContentFile 的对应物。这会将文件加载到当前工作区中,我可以像任何 pickle 文件一样从中读取它。最后,数据顺利加载到 cas_nums 中。

cas_strings = drive.CreateFile({'id':'1ZgZfEaKgqGnuBD40CY8zg0MCiqKmi1vH'})
print('title: %s, mimeType: %s' % (cas_strings['title'], cas_strings['mimeType']))
cas_strings.GetContentFile(cas_strings['title'])
cas_nums = pickle.load(open(cas_strings['title'],'rb'))

有关此的更多详细信息可以在下载文件内容部分的 pydrive 文档中找到 - http://pythonhosted.org/PyDrive/filemanagement.html#download-file-content

【讨论】:

    猜你喜欢
    • 2020-05-08
    • 2018-10-15
    • 1970-01-01
    • 2013-11-11
    • 2022-01-13
    • 1970-01-01
    • 2020-01-31
    • 1970-01-01
    相关资源
    最近更新 更多