1

I'm trying to write a Google Document script and it's going terribly. I am appending all of the information from specific rows to a list and I want to print out a certain item in that list. eg The second thing it adds to the list. However that would be easy right? But I'm having to append the list to a list. So I have a list of lists.

And Now I don't know how to get the script to just print out the 2nd item of each list inside of the list.

This is my code-

if not spreadsheet_key:
    print >>sys.stderr, 'No Spreadsheet Key given'
    sys.exit()

worksheet_id = 'od6'
storage = Storage(r'P:\Bureau Schedule\auto_drive_creds')
credentials = storage.get()
if credentials.access_token_expired:
    credentials.refresh(httplib2.Http())

spr_client = gdata.spreadsheet.service.SpreadsheetsService(
    additional_headers={'Authorization' : 'Bearer %s' % credentials.access_token}
    )

q = gdata.spreadsheet.service.CellQuery()
q.return_empty = 'true'

cells = spr_client.GetCellsFeed(
        spreadsheet_key,
        worksheet_id,
        query=q
        )

batchRequest = gdata.spreadsheet.SpreadsheetsCellsFeed()

lst_a = []

for i, cell in enumerate(cells.entry):
    lst_b = []
    if cell.title.text.startswith('AG'):
        if cell.content.text == '2':
            lst_b.append([cells.entry[i-30].cell.inputValue, cells.entry[i-29].cell.inputValue, cells.entry[i-25].cell.inputValue])
            lst_a.append(lst_b)

print lst_a[0]

I'm expecting quite an easy answer but I'm unsure and I've been stuck for a quite awhile so I thought I may as well ask.

Harry Adkins
  • 101
  • 1
  • 10

2 Answers2

0

Here's how to print out the 2nd item of each list inside of a list:

for sublist in list:
    print sublist[1]
Riley Martine
  • 193
  • 2
  • 9
  • I've tried that a couple of times. Either errors saying List index out of range or prints everything in the list of lists. – Harry Adkins May 09 '18 at 17:02
  • List index out of range would indicate that there isn't a second element in one of the sublists. Wrap the print statement in a try-except block and find the offending sublist. – Riley Martine May 09 '18 at 17:07
0

You can use a for loop and iterate through the list of lists and print the second item:

for sublist in lst_a: 
    print sublist[1]
sudormrfbin
  • 618
  • 8
  • 16
  • I've tried that a couple of times. Either errors saying List index out of range or prints everything in the list of lists. – Harry Adkins May 09 '18 at 17:02
  • You are using `list.append` instead of `list.extend`. Change `lst_a.append(...)` to `lst_a.extend(...)`, and do the same for `lst_b.append` – sudormrfbin May 09 '18 at 17:07
  • See https://stackoverflow.com/questions/252703/difference-between-append-vs-extend-list-methods-in-python – sudormrfbin May 09 '18 at 17:08
  • Just printing out 1 letter, I'm unsure on how it is but it's happening. I appreciate all the help by the way, I wasn't expecting the feedback. I can send you an email or something of my code or update a comment if you want? – Harry Adkins May 09 '18 at 17:13
  • Sorry, I made a mistake; you should only change `lst_b.append` to `lst_b.extend`. `lst_a.append(lst_b)` is alright. Hope it works now :) – sudormrfbin May 10 '18 at 02:09