【问题标题】:Google Spreadsheet to CSV in Google DriveGoogle 电子表格到 Google Drive 中的 CSV
【发布时间】:2019-09-26 07:16:40
【问题描述】:

在将 CSV 文件上传到 Google 驱动器时,它会自动转换为 Google 表格。如何将其保存为驱动器中的 CSV 文件?或者我可以通过熊猫数据框阅读谷歌表格吗?

开发环境:Google Colab

代码片段

输入

data = pd.read_csv("ner_dataset.desktop (3dec943a)", 
encoding="latin1").fillna(method="ffill")

data.tail(10)

输出

    [Desktop Entry]
0   Type=Link
1   Name=ner_dataset
2   URL=https://docs.google.com/spreadsheets/d/1w0...

【问题讨论】:

  • 这可能会对您有所帮助:gspread.readthedocs.io/en/latest

标签: python pandas google-sheets drive


【解决方案1】:

工作代码

from google.colab import auth
auth.authenticate_user()

import gspread
from oauth2client.client import GoogleCredentials

gc = gspread.authorize(GoogleCredentials.get_application_default())

worksheet = gc.open('Your spreadsheet name').sheet1

# get_all_values gives a list of rows.
rows = worksheet.get_all_values()
print(rows)

# Convert to a DataFrame and render.
import pandas as pd

pd.DataFrame.from_records(rows)

【讨论】:

    【解决方案2】:
    #Mount the Drive
    from google.colab import drive
    drive.mount('drive')
    
    #Authenticate you need to do with your credentials, fill yourself
    gauth = GoogleAuth()
    
    #Create CSV and Copy
    df.to_csv('data.csv')
    !cp data.csv drive/'your drive'
    

    【讨论】:

    • 感谢您的回复。以下代码为我工作。 ` from google.colab import auth auth.authenticate_user() import gspread from oauth2client.client import GoogleCredentials gc = gspread.authorize(GoogleCredentials.get_application_default()) worksheet = gc.open('你的电子表格名称').sheet1 # get_all_values 给出一个行列表。 rows = worksheet.get_all_values() print(rows) # 转换为 DataFrame 并渲染。将熊猫导入为 pd pd.DataFrame.from_records(rows) `
    • @Phani,您可以回答自己的问题,寻找代码的人可能会有所帮助。你可以贴下来。感谢您使用 stack over flow
    猜你喜欢
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 2012-12-14
    相关资源
    最近更新 更多