-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('')
3
  • 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. Commented Apr 22, 2020 at 15:17
  • @cagta but there is value (may 4) Commented Apr 22, 2020 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. Commented Apr 22, 2020 at 15:45

1 Answer 1

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".

Sign up to request clarification or add additional context in comments.

1 Comment

that [''] * (6-len(row)) thing was wery clever, thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.