1

I have a piece of code and it reads out the cell value of a cell in excel using openpyxl and I want to put those values in an array so I can use the specific value I want. How ever when I put my value in an array it behaves strangely.

This is the code:

import datetime,openpyxl,re
from openpyxl.styles import Font
from openpyxl.styles.colors import RED
wb = openpyxl.load_workbook("customers.xlsx", data_only = 'True')
print(wb.sheetnames)
sh = wb["FEB 2019"]

#FROM HERE TO 

def room():

    counter = 0

    for row in sh["A20":"A31"]:
        for cell in row:
            result=[0]*12
            result[counter] = cell.value
            #results=[cell.value() for x in range (13)]
            result[counter]= re.sub("[A-Z]+","",result[counter])
            result[counter]=result[counter].strip()
            counter += 1 
            print(result)
#HERE
def names():
    results=[""]*113
    for row in sh["B20":"H31"]:
        print("")
        #room()
        for cell in row:
            b = cell.value
            result = re.sub("[0-9]+","", str(b))
            result = re.sub(r'[£]',"",result)
            result = re.sub(r'[,]',"",result)
            result = re.sub(r'[.]',"",result)
            result = re.sub("BEDROOM","",result)
            result = re.sub("STUDIO","",result)
            result = re.sub("APARTMENT","",result)
            result = re.sub("COSY RM","",result)
            result = re.sub("COSY","",result)
            result = re.sub("GARDEN","",result)
            if result == "None" :
                None
            else:
                for counter in range(1):
                    print (result)
                    results[counter]=result
    #print(results[0])

#names()
room()

I put the whole code just for reference but I marked the areas that I am talking about in the code

This is my output:

['FEB 2019', 'JAN 2019', 'MARCH 2019', 'APRIL 2019', 'MAY 2019', 'JUNE 2019', 'JULY 2019', 'AUGUST 2019', 'SEPTEMBER 2019', 'OCTOBER 2019', 'NOVEMBER 2019', 'DECEMBER 2019']
['1', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, '2', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, '3', 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, '4', 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, '5', 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, '11', 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, '12', 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, '6', 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, '7', 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, '8', 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '9', 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '10']

If you try it on your own machine it might throw an error because of the module. To summarize my issue is that the code gives me this out put where as what I want is "1,2,3,4,5" and so in without the "0"s If you spot any other thing that I can work on or fix I am open to suggestions but the main issue is as stated. I could be able to share a sample of the file if required.

Edit the problem has been solved and this is how it looks right now:

def room():
    counter = 0
    result=[]*13
    for row in sh["A20":"A31"]:
        for cell in row:
            result.append(cell.value)
            result[counter] = re.sub("[A-Z]+","",(result[counter]))
            counter += 1 
    return (result)
KillerKingTR
  • 9
  • 1
  • 3
  • Replace your whole code block after `def room ... for cell in row: ...` with this on line of code => `result.append(cell.value)`. Therefore you have to define `result = []` outside the `for ...` loops. – stovfl Nov 29 '19 at 09:09
  • Thank you very much it resolved the issue – KillerKingTR Nov 29 '19 at 21:24

0 Answers0