-1

So I have this google sheets file, I need to extract event data and create Event Model in Django. So far I have no problem getting data from API, but some of the fields in spreadsheets are empty, thus, API does not return me those fields, for example, index 23 is complete, but in index 24 fields are not defined. It is ok for me to enter empty data in Django models, it does not matter at all. enter image description here

enter image description here

WHAT I ACTUALLY WANT is if array[22][1] is empty(which it is (array[22][0] is 'May 4')) then append null value for that index. I wrote this line but it doesn't work, how do I implement this?

for row in range(index):
        for column in range(6):
            try:
                print(values[row][column])
            except IndexError:
                values[row][column].append('')
Joshua Hall
  • 332
  • 4
  • 15
Kenan
  • 141
  • 2
  • 11
  • I think what you need is to use values[row].append(''). Since you don't have values[22][1] you can not append anything on it. –  Apr 22 '20 at 15:17
  • @cagta but there is value (may 4) – Kenan Apr 22 '20 at 15:38
  • Yes but it's the value of the values[22][0] it's not related to values[22][1]. values[22] = ['May 4'] so you want to convert it to ['May 4', ' '], For that purpose you should append values[22] list. –  Apr 22 '20 at 15:45

1 Answers1

1

If row[column] is missing, you want to append to row, not row[column] itself (which we've already established is missing, and will get you a TypeError or something).

Another option would be something like:

for row in values:
    if len(row) < 6:
        row.extend([''] * (6 - len(row)))

i.e. "for each row, if it's shorter than 6 items, add enough '' items to make up the difference".

Samwise
  • 68,105
  • 3
  • 30
  • 44