如何使用python中的gdata在公共电子表格中获取特定工作表的行

时间:2014-11-04 07:19:26

标签: python python-2.7 google-sheets gdata google-data

当我尝试使用以下代码时,我在python上使用gdata从公共电子表格中读取特定工作表的行

client = gdata.spreadsheet.service.SpreadsheetsService()  
   key = 'xxxxxxxxxxxxxxxxxxxxxxxxxx'  
   worksheets_feed = client.GetWorksheetsFeed(key, visibility='public', projection='values')  
   # print worksheets_feed
   for entry in worksheets_feed.entry:
       print entry.title.text
       worksheet_id = entry.id.text.rsplit('/',1)[1]
       rows = client.GetListFeed(key, worksheet_id).entry

将错误视为

Traceback (most recent call last):

  File "lib/scrapper.py", line 89, in <module>

    start_it() 

  File "lib/scrapper.py", line 56, in start_it

    rows = client.GetListFeed(key, worksheet_id).entry

  File "/Library/Python/2.7/site-packages/gdata/spreadsheet/service.py", line 252, in GetListFeed

    converter=gdata.spreadsheet.SpreadsheetsListFeedFromString)

  File "/Library/Python/2.7/site-packages/gdata/service.py", line 1074, in Get

    return converter(result_body)

  File "/Library/Python/2.7/site-packages/gdata/spreadsheet/__init__.py", line 474, in SpreadsheetsListFeedFromString

    xml_string)

  File "/Library/Python/2.7/site-packages/atom/__init__.py", line 93, in optional_warn_function

    return f(*args, **kwargs)

  File "/Library/Python/2.7/site-packages/atom/__init__.py", line 127, in CreateClassFromXMLString

    tree = ElementTree.fromstring(xml_string)

  File "<string>", line 125, in XML

cElementTree.ParseError: no element found: line 1, column 0

有人可以纠正我错在哪里

1 个答案:

答案 0 :(得分:1)

尝试:

worksheet_feed = spreadsheet.GetWorksheetsFeed(spreadsheetId)
worksheetfeed = []
for worksheet in worksheet_feed.entry:
    worksheetfeed.append(worksheet.id.text.rsplit('/', 1)[0]) 
list_feed = spreadsheet.GetListFeed(spreadsheetId, worksheetfeed[0])#get first worksheet
entryList = []
for entry in list_feed.entry:
    tempDict = {}
    for key in entry.custom:
        tempDict[str(key)] = str(entry.custom[key].text)

已定义spreadsheetId并且您之前已经过身份验证。