简体   繁体   中英

How to get the rows of specific worksheet on public spreadsheet using gdata in python

I am using gdata on python to read the rows of specific worksheet from public spreadsheet when i tried the following code

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

getting the error as

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

can somebody correct me where i am wrong

Try:

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)

where spreadsheetId has been defined and you have been previously authenticated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM