Python 3-将google电子表格保存为csv
我正在尝试将一个私有的谷歌电子表格保存到csv中。我找到了另一条解决这个问题的线索(),但是答案可以追溯到2012年。有一个解决方案,我已经尝试使用,但我遇到了错误。这是我目前正在使用的代码Python 3-将google电子表格保存为csv,python,csv,python-3.x,google-docs,Python,Csv,Python 3.x,Google Docs,我正在尝试将一个私有的谷歌电子表格保存到csv中。我找到了另一条解决这个问题的线索(),但是答案可以追溯到2012年。有一个解决方案,我已经尝试使用,但我遇到了错误。这是我目前正在使用的代码 import csv import gspread // used the actual username, password and doc-id in the python script username = "username" password = "password" docid = "doc-
import csv
import gspread
// used the actual username, password and doc-id in the python script
username = "username"
password = "password"
docid = "doc-id"
client = gspread.login(username, password)
spreadsheet = client.open_by_key(docid)
for i, worksheet in enumerate(spreadsheet.worksheets()):
filename = docid + '-worksheet' + str(i) + '.csv'
with open(filename, 'wb') as f:
writer = csv.writer(f)
writer.writerows(worksheet.get_all_values())
这就是IDLE抛出的错误
writer.writerows(worksheet.get_all_values())
TypeError: 'str' does not support the buffer interface
您一定已经意识到,我对python非常陌生,我正在尝试解决这个问题。有人能指出我使用的代码有什么问题吗?在Python 3中,以文本模式打开文件,并将
换行符设置为'
,让CSV编写器控制要编写的换行符:
with open(filename, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(worksheet.get_all_values())
(2016年7月)现在访问Google Sheets文件时,GData的使用已经过时。当前将Google工作表导出为CSV的现代方式是使用Google Drive API。我用一个Python 2+3代码示例编写了一个示例,向您展示了如何做到这一点。还检查了另一个线程,在那里我添加了一个比2012年更现代的答案。有了这个更新,这个线程应该被标记为另一个线程的副本。要从Python更一般的意义上访问Google工作表,请查看相关问题。这就像一种魅力。回答的速度惊人。非常感谢D