[英]How can I format data from gspread api into Google Sheets
I am new to python (and programming in general).我是 python(和一般编程)的新手。 I am currently working on automating a data entry aspect of my job.我目前正在致力于自动化我工作的数据输入方面。 So far I have figured out how to sort out the data I want from a csv with pandas, and load it into the appropriate area of a google spreadsheet with the gspread api.到目前为止,我已经弄清楚如何从带有 pandas 的 csv 中整理出我想要的数据,并使用 gspread Z8A5DA52ED126447D359E70C05721A8 将其加载到谷歌电子表格的适当区域中。 My problem is that the csv data dumps into a single cell of the speadsheet.我的问题是 csv 数据转储到电子表格的单个单元格中。 How can I make have it enter across the row of cells?我怎样才能让它进入整个单元格行? I have tried every parameter of the.appendrow() function.我已经尝试了 .appendrow() function 的每个参数。
Thanks so much - Owen K非常感谢 - 欧文 K
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("/Users/macbook/Documents/Atom Projects/Sheets1/creds.json", scope)
client = gspread.authorize(creds)
import pandas as pd
df = pd.read_csv('/Users/macbook/Documents/Atom Projects/Sheets1/report.csv')#, index_col = '(Child) ASIN')
Luna = (df['(Child) ASIN'] == 'B0716JTGSX')
LU = df[Luna].to_csv()
print(LU)
bick4 = client.open("Test Sheet") .worksheet('bick 4')
bick4.append_row([LU])
I believe your goal as follows.我相信你的目标如下。
LU = df[Luna].to_csv()
to each cell in the Google Spreadsheet.您想将LU = df[Luna].to_csv()
的值放入 Google 电子表格中的每个单元格。For this, how about this answer?为此,这个答案怎么样?
df[Luna]
and the list is put to the Spreadsheet using gspread.在这种情况下,从df[Luna]
创建一个列表,并使用 gspread 将该列表放入电子表格中。I propose the following 2 patterns.我提出以下两种模式。
In this pattern, the value of df[Luna]
is put to the Spreadsheet using the method of values_update
.在此模式中,使用values_update
方法将df[Luna]
的值放入电子表格。
client = gspread.authorize(creds)
spreadsheet = client.open('Test Sheet')
sheet_name = 'bick 4'
import pandas as pd
df = pd.read_csv('/Users/macbook/Documents/Atom Projects/Sheets1/report.csv')#, index_col = '(Child) ASIN')
Luna = (df['(Child) ASIN'] == 'B0716JTGSX')
LU = df[Luna]
values = [LU.columns.values.tolist()]
values.extend(LU.values.tolist())
spreadsheet.values_update(sheet_name, params={'valueInputOption': 'USER_ENTERED'}, body={'values': values})
In this pattern, the value of df[Luna]
is put to the Spreadsheet using the method of values_update
.在此模式中,使用values_update
方法将df[Luna]
的值放入电子表格。
client = gspread.authorize(creds)
spreadsheet = client.open('Test Sheet')
sheet_name = 'bick 4'
import pandas as pd
df = pd.read_csv('/Users/macbook/Documents/Atom Projects/Sheets1/report.csv')#, index_col = '(Child) ASIN')
Luna = (df['(Child) ASIN'] == 'B0716JTGSX')
LU = df[Luna]
values = LU.values.tolist()
spreadsheet.values_append(sheet_name, params={'valueInputOption': 'USER_ENTERED'}, body={'values': values})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.