(gspread): .get_all_records returns unordered dictionary

2.5k views Asked by At

I am accessing a python file via python. The google sheets looks like the following: enter image description here

But when I access it via:

    self.probe=[]
    self.scope = ['https://spreadsheets.google.com/feeds']
    self.creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', self.scope)
    self.client = gspread.authorize(self.creds)
    self.sheet = self.client.open('Beziehende').sheet1
    self.probe = self.sheet.get_all_records()
    print(self.probe)

it results in enter image description here Ho can I get the results in the same order as they are written in the google sheet? Thank you for your help.

Edit Sorry, here are some more information. My program has two functions: 1.) It can check if a name / address etc. is already in the database. If the name is in the database, it prints all the information about that person. 2.) It lets me add people's information to the database.

The Problem: I am loading the whole database into the list and later writing it all back. But when writing it back, the order gets messed up, as the get_all_records stored it in a random order. (This is the very first program I have ever written by myself, so please forgive the bad coding).

I wanted to know if there is a possibility to get the data in order. but if not, than I just have to find a way, online to write the newest entry (which is probably more efficient anyway I guess...)

    def create_window(self):

    self.t = Toplevel(self)
    self.t.geometry("250x150")

    Message(self.t, text="Name", width=100, anchor=W).grid(row=1, column=1)
    self.name_entry = Entry(self.t)
    self.name_entry.grid(row=1, column=2)
    Message(self.t, text="Adresse", width=100, anchor=W).grid(row=2, column=1)
    self.adr_entry = Entry(self.t)
    self.adr_entry.grid(row=2, column=2)
    Message(self.t, text="Organisation", width=100, anchor=W).grid(row=3, column=1)
    self.org_entry = Entry(self.t)
    self.org_entry.grid(row=3, column=2)
    Message(self.t, text="Datum", width=100, anchor=W).grid(row=4, column=1)
    self.date_entry = Entry(self.t)
    self.date_entry.grid(row=4, column=2)
    self.t.button = Button(self.t, text="Speichern", command=self.verify).grid(row=5, column=2)


    #name


    #window = Toplevel(self.insert_window)
def verify(self):
    self.ver = Toplevel(self)
    self.ver.geometry("300x150")
    self.ver.grid_columnconfigure(1, minsize=100)
    Message(self.ver, text=self.name_entry.get(), width=100).grid(row=1, column=1)
    Message(self.ver, text=self.adr_entry.get(), width=100).grid(row=2, column=1)
    Message(self.ver, text=self.org_entry.get(), width=100).grid(row=3, column=1)
    Message(self.ver, text=self.date_entry.get(), width=100).grid(row=4, column=1)
    confirm_button=Button(self.ver, text='Bestätigen', command=self.data_insert).grid(row=4, column=1)
    cancle_button=Button(self.ver, text='Abbrechen', command=self.ver.destroy).grid(row=4, column=2)



def data_insert(self):
    new_dict = collections.OrderedDict()
    new_dict['name'] = self.name_entry.get()
    new_dict['adresse'] = self.adr_entry.get()
    new_dict['organisation'] = self.org_entry.get()
    new_dict['datum'] = self.date_entry.get()
    print(new_dict)
    self.probe.append(new_dict)
    #self.sheet.update_acell('A4',new_dict['name'])
    self.update_gsheet()
    self.ver.destroy()
    self.t.destroy()

def update_gsheet(self):
    i = 2
    for dic_object in self.probe:
        j = 1
        for category in dic_object:
            self.sheet.update_cell(i,j,dic_object[category])
            j += 1
        i += 1

def search(self):
    print(self.probe)
    self.result = []
    self.var = self.entry.get()            #starting index better
    self.search_algo()
    self.outputtext.delete('1.0', END)
    for dict in self.result:
        print(dict['Name'], dict['Adresse'], dict['Organisation'])
        self.outputtext.insert(END, dict['Name'] + '\n')
        self.outputtext.insert(END, dict['Adresse']+ '\n')
        self.outputtext.insert(END, dict['Organisation']+ '\n')
        self.outputtext.insert(END, 'Erhalten am '+dict['Datum']+'\n'+'\n')
    if not self.result:
        self.outputtext.insert(END, 'Name not found')
        return FALSE
    return TRUE



def search_algo(self):

    category = self.v.get()
    print(category)
    for dict_object in self.probe:
        if dict_object[category] == self.var:
            self.result.append(dict_object)
2

There are 2 answers

1
Daniel Roseman On

I'm not familiar with gspread, which appears to be a third-party client for the Google Sheets API, but it looks like you should be using get_all_values rather than get_all_records. That will give you a list of lists, rather than a list of dicts.

0
Philip On

Python dictionaries are unordered. There is the OrderedDict in collections, but hard to say more about what the best course of action should be without more insight into why you need this dictionary ordered...