我的这个问题说明了如何Getting Google Spreadsheet CSV into A Pandas Dataframe
正如评论家之一注意到你有没有要求在CSV格式的数据,你必须在URL 您可以使用此代码末端的“编辑”请求并看到它在电子表格上工作(顺便需要公开..)也可以做私人表单,但这是另一个话题。
from StringIO import StringIO # got moved around in python3 if you're using that.
import requests
r = requests.get('https://docs.google.com/spreadsheet/ccc?key=0Ak1ecr7i0wotdGJmTURJRnZLYlV3M2daNTRubTdwTXc&output=csv')
data = r.content
In [10]: df = pd.read_csv(StringIO(data), index_col=0,parse_dates=['Quradate'])
In [11]: df.head()
Out[11]:
City region Res_Comm \
0 Dothan South_Central-Montgomery-Auburn-Wiregrass-Dothan Residential
10 Foley South_Mobile-Baldwin Residential
12 Birmingham North_Central-Birmingham-Tuscaloosa-Anniston Commercial
38 Brent North_Central-Birmingham-Tuscaloosa-Anniston Residential
44 Athens North_Huntsville-Decatur-Florence Residential
mkt_type Quradate National_exp Alabama_exp Sales_exp \
0 Rural 2010-01-15 00:00:00 2 2 3
10 Suburban_Urban 2010-01-15 00:00:00 4 4 4
12 Suburban_Urban 2010-01-15 00:00:00 2 2 3
38 Rural 2010-01-15 00:00:00 3 3 3
44 Suburban_Urban 2010-01-15 00:00:00 4 5 4
用于获取CSV输出新的谷歌电子表格网址格式
https://docs.google.com/spreadsheets/d/177_dFZ0i-duGxLiyg6tnwNDKruAYE-_Dd8vAQziipJQ/export?format=csv&id
那么,他们改变了URL格式再次小幅现在你需要:
https://docs.google.com/spreadsheets/d/177_dFZ0i-duGxLiyg6tnwNDKruAYE-_Dd8vAQziipJQ/export?format=csv&gid=0 #for the 1st sheet
我还发现,我需要要做到以下几点来处理Python 3对上述的轻微修改:
from io import StringIO
,并获得该文件:
guid=0 #for the 1st sheet
act = requests.get('https://docs.google.com/spreadsheets/d/177_dFZ0i-duGxLiyg6tnwNDKruAYE-_Dd8vAQziipJQ/export?format=csv&gid=%s' % guid)
dataact = act.content.decode('utf-8') #To convert to string for Stringio
actdf = pd.read_csv(StringIO(dataact),index_col=0,parse_dates=[0], thousands=',').sort()
actdf现在是一个完整的大熊猫与数据框标题(列名)
您可以发布数据的误差线,被田野正确界定?这通常是这些错误的来源 – EdChum
你确定那个URL甚至*以可访问的格式返回数据吗?这不像它产生一个HTML表格来解析。 – DSM
您在请求结束时是否需要“&output = csv”?另请参阅[谷歌电子表格熊猫](http://stackoverflow.com/questions/19611729/getting-google-spreadsheet-csv-into-a-pandas-dataframe) – chrisb