簡體   English   中英

在Google colaboratory上使用pydrive加載pickle文件時出現“ UnicodeDecodeError:'utf-8'編解碼器無法解碼字節0x80”的消息

[英]“UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80” while loading pickle file using pydrive on google colaboratory

我是第一次使用google colaboratory(colab)和pydrive。 我正在嘗試使用colab將數據加載到在我的Google驅動器中特定目錄中的pickle文件中寫入的“ CAS_num_strings”中,使用colab為:

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'))

其中'id':'1UEqIADV_tHic1Le0zlT25iYB7T6dBpBj'確保它具有此ID所指定的特定父文件夾。 最后的打印命令給我輸出:

'1ZgZfEaKgqGnuBD40CY8zg0MCiqKmi1vH'

因此,我能夠創建和轉儲其ID為“ 1ZgZfEaKgqGnuBD40CY8zg0MCiqKmi1vH”的泡菜文件。 現在,出於其他目的,我想在另一個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'選項進行的正常泡菜轉儲和加載過程中均能順利進行。 但是在當前情況下,轉儲后,我似乎無法從上一步創建的google驅動器中的pickle文件中加載它。 錯誤存在於我的某個地方,無法在“ return self.content.getvalue()。decode(encoding)”處指定如何解碼數據。 我似乎無法從這里( https://developers.google.com/drive/v2/reference/files#resource-representations )找到要修改的關鍵字/元數據標記。 任何幫助表示贊賞。 謝謝

問題在於,僅當內容為有效的UTF-8字符串( docs )時, GetContentString才有效,而您的泡菜則無效。

不幸的是,由於沒有GetContentBytes ,您將不得不做一些額外的工作-您必須將內容保存到文件中並讀出來。 這是一個工作示例: https : //colab.research.google.com/drive/1gmh21OrJL0Dv49z28soYq_YcqKEnaQ1X

實際上,在朋友的幫助下,我找到了一個很好的答案。 我使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM