简体   繁体   English

在python中使用gspread import_csv函数时找不到文件

[英]File not found when using gspread import_csv function in python

I am using gspread package in python.我在 python 中使用 gspread 包。

I try to import a csv into a google spreadsheet but I got an error.我尝试将 csv 导入 Google 电子表格,但出现错误。

My code is as follows:我的代码如下:

import gspread
from oauth2client.service_account import ServiceAccountCredentials

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

The error I got is:我得到的错误是:

---------------------------------------------------------------------------
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"
 }
}

However, I get no error when I try to read or write on cell directly (with update_cell function for example), so the spreadsheet does exist and I can write on it.但是,当我尝试直接在单元格上读取或写入(例如使用update_cell函数)时,我没有收到任何错误,因此电子表格确实存在并且我可以在其上书写。 It is specifically import_csv that throws an error.尤其是import_csv会引发错误。

I have created the google sheet through the google drive web interface.我已经通过谷歌驱动器网络界面创建了谷歌表。 I then added my client_email from credential.json (blabla@blabla-220209.iam.gserviceaccount.com) in the authorized mail of the spreadsheet.然后,我在电子表格的授权邮件中添加了来自 credential.json (blabla@blabla-220209.iam.gserviceaccount.com) 的 client_email。

Any suggestion ?有什么建议吗?

I fixed it by sharing the file with the 'client_email' found within the credentials JSON file.我通过与凭证 JSON 文件中的“client_email”共享文件来修复它。 Although I did not test this, I also suspect making the spreadsheet Public (anyone on web can see) will fix the issue.虽然我没有对此进行测试,但我也怀疑将电子表格设为公开(网络上的任何人都可以看到)将解决该问题。

Cheers!干杯!

By going to your sheet and changing sharing options to anyone with the link can edit, fixes it.通过转到您的工作表并将共享选项更改为具有链接的任何人都可以编辑,修复它。

Here's the code I used:这是我使用的代码:

import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ["(some scopes)"]
creds = ServiceAccountCredentials.from_json_keyfile_name("creds.json", scope)
gc = gspread.service_account("creds.json", scope)
client = gspread.authorize(creds)
sLink = "(link)"
sh = gc.open_by_url(sLink)
sh.share('<example.email.com>', perm_type='user', role='writer')

Make sure you're using the latest version of gspread.确保您使用的是最新版本的 gspread。 The one that is eg bundled with Google Colab is outdated:例如,与 Google Colab 捆绑在一起的那个已经过时了:

!pip install --upgrade gspread

This fixed the error in gs.csv_import for me.这为我修复了gs.csv_import的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM