在python中使用gspread import_csv函数时找不到文件 [英] File not found when using gspread import_csv function in python

查看:110
本文介绍了在python中使用gspread import_csv函数时找不到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在python中使用gspread软件包.

I am using gspread package in python.

我尝试将csv导入到Google电子表格中,但是出现错误.

I try to import a csv into a google spreadsheet but I got an error.

我的代码如下:

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)

我得到的错误是:

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

但是,当我尝试直接在单元格上进行读写时(例如,使用 update_cell 函数),我没有出现任何错误,因此电子表格确实存在并且可以在其上书写.专门是 import_csv 会引发错误.

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. It is specifically import_csv that throws an error.

我已经通过Google云端硬盘网络界面创建了Google工作表.然后,我将来自credential.json的client_email(blabla@blabla-220209.iam.gserviceaccount.com)添加到了电子表格的授权邮件中.

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.

有什么建议吗?

推荐答案

请确保您使用的是最新版本的gspread.例如与Google Colab捆绑销售的商品已过时:

Make sure you're using the latest version of gspread. The one that is e.g. bundled with Google Colab is outdated:

!pip install --upgrade gspread

这为我修复了 gs.csv_import 中的错误.

This fixed the error in gs.csv_import for me.

这篇关于在python中使用gspread import_csv函数时找不到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆