[英]Python 3 - Saving a google spreadsheet as a csv
我正在嘗試將私有的Google電子表格保存到csv中。 我找到了另一個解決此問題的線程( 使用Python從Google Docs下載電子表格 ),但是答案可以追溯到2012年。我曾嘗試使用一種解決方案,但遇到了錯誤。 這是我目前正在使用的代碼
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中,以文本模式打開文件,並將newline
設置為''
以讓CSV編寫器控制要寫入的換行符:
with open(filename, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(worksheet.get_all_values())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.