Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将google sheet csv作为字典加载_Python_Google Sheets - Fatal编程技术网

在python中将google sheet csv作为字典加载

在python中将google sheet csv作为字典加载,python,google-sheets,Python,Google Sheets,我有一个公共谷歌表,有两张 我知道如果只有一张表,如何将其设置为csv,但是有没有一种简单的方法可以将表1和表2作为字典或csv文件,将每张表作为一个csv文件,我将对其进行解析。 两张纸的gid不同 最后,我将标题作为键,标题下的值作为值,只需使用pandas加载CSV即可。然后你可以转换成Dict或其他任何东西 import pandas as pd # Read data from file 'filename.csv' data = pd.read_csv("filename.csv

我有一个公共谷歌表,有两张 我知道如果只有一张表,如何将其设置为csv,但是有没有一种简单的方法可以将表1和表2作为字典或csv文件,将每张表作为一个csv文件,我将对其进行解析。 两张纸的gid不同


最后,我将标题作为键,标题下的值作为值,只需使用pandas加载CSV即可。然后你可以转换成Dict或其他任何东西

import pandas as pd 
# Read data from file 'filename.csv' 
data = pd.read_csv("filename.csv") 

data.to_dict('series')
使用请求库下载每张工作表,并将响应内容写入文件

工作执行:

import requests

sheets = {
    'sheet1': 'https://docs.google.com/spreadsheets/d/14hFn00O9632n96Z2xGWvfrcY-K4kHiOGR02Rx7dsj54/export?format=csv&id=14hFn00O9632n96Z2xGWvfrcY-K4kHiOGR02Rx7dsj54&gid=0',
    'sheet2': 'https://docs.google.com/spreadsheets/d/14hFn00O9632n96Z2xGWvfrcY-K4kHiOGR02Rx7dsj54/export?format=csv&id=14hFn00O9632n96Z2xGWvfrcY-K4kHiOGR02Rx7dsj54&gid=447738801'
} 

for sheet in list(sheets.keys()):
    response = requests.get(sheets[sheet])
    with open(f'{sheet}.csv', 'wb') as csvfile:
        csvfile.write(response.content)
在这种情况下,这会将每张图纸保存在sheet1.csv和sheet2.csv文件中。请注意,我只是通过从浏览器下载CSV并复制下载链接来获得每张图纸的链接


然后可以使用CSV库将其转换为字典。请参阅。

谢谢@glhr这基本上解决了我的问题,我们是否有办法将response.content仅保留在内存中,而不将文件写入磁盘,然后再次读取以创建dict。这是可能的,请在您的帖子中解释您希望如何在字典中准确地格式化数据,例如,您希望每一行都有一个字典吗?添加到帖子中,每个cloumn头是一个键,下面的值是它的值,我们尝试了flike=io.StringIOresponse.text然后在csv中为行设置格式。readerflike:每个键不能有多个值,除非这些值在一个列表中。哦,是的,那么我不知道如何将它们保留为一个列表:header是键,其他列作为header的值,header是一个列表,用于下载文件后要做的其他事情,谢谢帮助