我在 python 中使用 gspread 包。我嘗試將 csv 導(dǎo)入 Google 電子表格,但出現(xiàn)錯(cuò)誤。我的代碼如下:import gspreadfrom oauth2client.service_account import ServiceAccountCredentialsspreadsheet_id = '1tFhK2-zebkG1fZFF6Xe5LHyONkh97ANOkcf'scopes = [ 'https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']creds = ServiceAccountCredentials.from_json_keyfile_name('./credentials.json', scopes=scopes)client = gspread.authorize(creds)file = open('test_csv.csv',mode='r')csv = file.read()file.close()client.import_csv(spreadsheet_id, csv)我得到的錯(cuò)誤是:---------------------------------------------------------------------------APIError Traceback (most recent call last)<ipython-input-264-eb5d4ce7bc69> in <module>()----> 1 client.import_csv(spreadsheet_id, csv)~/anaconda3/lib/python3.6/site-packages/gspread/client.py in import_csv(self, file_id, data) 238 'convert': True 239 },--> 240 headers=headers 241 ) 242 ~/anaconda3/lib/python3.6/site-packages/gspread/client.py in request(self, method, endpoint, params, data, json, files, headers) 77 return response 78 else:---> 79 raise APIError(response) 80 81 def list_spreadsheet_files(self):APIError: { "error": { "errors": [ { "domain": "global", "reason": "notFound", "message": "File not found: 1tFhK2-zebkG1fZFF6Xe5LHyONkh97ANOkcf", "locationType": "other", "location": "file" } ], "code": 404, "message": "File not found: 1tFhK2-zebkG1fZFF6Xe5LHyONkh97ANOkcf" }}但是,當(dāng)我嘗試直接在單元格上讀取或?qū)懭耄╱pdate_cell例如使用函數(shù))時(shí),我沒有收到任何錯(cuò)誤,因此電子表格確實(shí)存在并且我可以在其上書寫。特別import_csv是拋出錯(cuò)誤。我已經(jīng)通過谷歌驅(qū)動(dòng)器網(wǎng)絡(luò)界面創(chuàng)建了谷歌表。然后,我在電子表格的授權(quán)郵件中添加了來自 credential.json (blabla@blabla-220209.iam.gserviceaccount.com) 的 client_email。有什么建議嗎?
在python中使用gspread import_csv函數(shù)時(shí)找不到文件
慕碼人8056858
2021-07-13 13:01:00