A
A
aCL2014-03-28 13:43:43
Google
aCL, 2014-03-28 13:43:43

Problems involving gspread in python

Good day
A copy of my post from python.su :)
Introduction:
There is a Google Spreadsheet (hereinafter just a table), columns Number, Balance, and a number of cells
There is a script. It implements a number parameter class, incl. attributes row (number of the row in which the number is located in the table) and cells (list of table cells located on the same line as the number)
There is a base dictionary in which the key is the phone number, and the value is an instance of the number parameters class.
I am using the gspread module.
wks_range - list of all table cells. One element of the list is an instance of the gspread.cell class with row, cell and value attributes. If you display one of the elements of the list, it will look like this:
Obviously, R is the number of the row where the cell is located, C is the number of the column, row is the value of the cell
Part of the script:

for num in base.keys():
    for cell in wks_range:
        if base[num].row==cell.row:
            base[num].cells.append(cell)#ячейки, относящиеся к номеру

It is assumed that the maximum length of the base.cells list cannot be more than 4. In fact, the list of cells of class instances (I assume that on all numbers - during the tests I checked one randomly selected key) contains all the cells of the table.
If in interactive mode, after running the script, checking and detecting an error, run the following:
>>> for num in base.keys():
  base[num].cells=[]
  
>>> for num in base.keys():
  for cell in wks_range:
    if base[num].row==cell.row and base[num].row!=None:
      base[num].cells.append(cell)
  if len(base[num].cells)>4:
    print(num)

Then no results are displayed, i.e. in interactive mode, the maximum length of the list is what it should be.
More nuances:
for num in list(base.keys())[:30]:
    for cell in wks_range:
        if base[num].row==cell.row:
            base[num].cells.append(cell)

I output the result (part removed):
>>> base['9030064337'].cells
[<Cell R220C1 '9037551123'>, <Cell R220C2 '-1782.88'>, <Cell R220C3 None>, <Cell R220C4 None>, <Cell R51C1 '9030064337'>, <Cell R51C2 '-5003.47'>, <Cell R51C3 None>, <Cell R51C4 None>, <Cell R130C1 '9055669265'>...

and almost the same code, only not append, but print:
...
if base[num].row==cell.row:
            print('{} {} {}'.format(num,base[num].row,cell))
9030064337 51 <Cell R51C1 '9030064337'>
9030064337 51 <Cell R51C2 '-5003.47'>
9030064337 51 <Cell R51C3 None>
9030064337 51 <Cell R51C4 None>

some problem in append'e? or where? broke my head...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question