Updating python gspread list with empty string after index is removed

FranzLich

I am creating a bot in discord for organizing lists then updates the lists to google sheet via gspread. The lists updated fine, however, if someone removed themselves from the list, the corresponding cell does not get removed, unless a new person replaced them in the list. I think its because of how python deals with list where empty spaces are simply.. nothing therefore gspread just ignores it. Therefore, my question is how do I force gspread to update every cells with an empty string should the list doesn't contains one? Thanks.

# Some codes

import gspread

gc = gspread.service_account(filename = 'a json')
sh = gc.open_by_key("a key")


# My lists
list1 = ["a","b","c"]
list2 = ["d","e","f"]

list_of_lists = [list1, list2]

# Update list into sheet

sh.values_update(
                'Sheet1!A1:C2', # fixed cells
                params={'valueInputOption': 'RAW'},
                body={'values': list_of_lists}
            )

output

old

# Update with new list

list1.remove("a")
list2.remove("d")

# Rerun list update
sh.values_update(
                'Sheet1!A1:C2', # fixed cells
                params={'valueInputOption': 'RAW'},
                body={'values': list_of_lists}
            )

New output

new

Wanted this output

wanted

Tanaike

In this answer, a dummy list including the empty values is used. And, the values are put using the method of batch_update in class gspread.models.Worksheet. In this case, one API call is used.

Modified script:

sh = gc.open_by_key("a key")
sheetName = 'Sheet1'  # Please set the sheet name.
worksheet = sh.worksheet(sheetName)

# My lists
list1 = ["a", "b", "c"]
list2 = ["d", "e", "f"]

list_of_lists = [list1, list2]
dummy = [[''] * len(e) for e in list_of_lists]

list1.remove("a")
list2.remove("d")

worksheet.batch_update([{
    'range': 'A1:C2',
    'values': dummy,
}, {
    'range': 'A1:C2',
    'values': list_of_lists,
}])

Reference:

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Empty list index error

分類Dev

python split empty string

分類Dev

Add a string after 4 characters for each element of a list in Python

分類Dev

Java: Find the index of a String in a list

分類Dev

Python: pop from empty list

分類Dev

After creating a .deb: dpkg:warning while removing, directory /usr/local/bin not empty so not removed

分類Dev

Why empty output after processing pointer to string?

分類Dev

Updating a string using regular expressions in Python

分類Dev

Why is a list containing an empty string truthy?

分類Dev

AngularJS and JAX-RS - Updating Table List after inserting model

分類Dev

Python - List Index Out Of Range -

分類Dev

Python string index and character comparing

分類Dev

Python – Convert string to list

分類Dev

Python Format String with List

分類Dev

Python -lxml xpath returns empty list

分類Dev

A star is being printed for an empty directory after running a script to list the subfolder

分類Dev

Why elements are removed after iframe

分類Dev

Rooms are not removed after everyone leaves

分類Dev

Android Java exception even after string null or empty check

分類Dev

C# List<string> add index to list console output

分類Dev

Python: strange list behaviour when initialising empty nested list

分類Dev

Select empty value in dropdown list with jQuery if not contains string

分類Dev

represent an index inside a list as x,y in python

分類Dev

Merging list of multi-index series in Python

分類Dev

Finding index of sublist in list python 3

分類Dev

How to append the list in Python for certain index?

分類Dev

Python find the index of a multi value List?

分類Dev

List Index out of Range Python Function

分類Dev

Python find the index of a multi value List?

Related 関連記事

  1. 1

    Empty list index error

  2. 2

    python split empty string

  3. 3

    Add a string after 4 characters for each element of a list in Python

  4. 4

    Java: Find the index of a String in a list

  5. 5

    Python: pop from empty list

  6. 6

    After creating a .deb: dpkg:warning while removing, directory /usr/local/bin not empty so not removed

  7. 7

    Why empty output after processing pointer to string?

  8. 8

    Updating a string using regular expressions in Python

  9. 9

    Why is a list containing an empty string truthy?

  10. 10

    AngularJS and JAX-RS - Updating Table List after inserting model

  11. 11

    Python - List Index Out Of Range -

  12. 12

    Python string index and character comparing

  13. 13

    Python – Convert string to list

  14. 14

    Python Format String with List

  15. 15

    Python -lxml xpath returns empty list

  16. 16

    A star is being printed for an empty directory after running a script to list the subfolder

  17. 17

    Why elements are removed after iframe

  18. 18

    Rooms are not removed after everyone leaves

  19. 19

    Android Java exception even after string null or empty check

  20. 20

    C# List<string> add index to list console output

  21. 21

    Python: strange list behaviour when initialising empty nested list

  22. 22

    Select empty value in dropdown list with jQuery if not contains string

  23. 23

    represent an index inside a list as x,y in python

  24. 24

    Merging list of multi-index series in Python

  25. 25

    Finding index of sublist in list python 3

  26. 26

    How to append the list in Python for certain index?

  27. 27

    Python find the index of a multi value List?

  28. 28

    List Index out of Range Python Function

  29. 29

    Python find the index of a multi value List?

ホットタグ

アーカイブ