.format join str int error

Hi, in my quest to confuse things I’ve been looking at finding the adjacent cell in an excel file when a cell in the relative cell is found(eventually looking to fill in text boxes based on a drop down). What code i have works if the data in column D is a string if its an integer i get the error
excelerror
the code snippet is

 ###Check status of variable#####
        
cells = (str(cellsOfFoundRow[3].value))
if type(cells) == str:
    print("It's a string")
    print(cellsOfFoundRow[3].value)
else :
    print("Not  a string")
    print(cellsOfFoundRow[3].value)

##############################
       
#for debugging purposes
input("stop here to check values")

if cellsOfFoundRow:
  
    print("Found:{}, the value of Column D is {}"
          .format(" ".join([cell.value for cell in cellsOfFoundRow]),
                  cellsOfFoundRow[3].value))

the error seems to be in the part cellsOfFoundRow[3].value), i’ve tried casting it as a string in the format section and also before where i tried to create the variable cells and put the variable in place of cellsOfFoundRow[3].value) but still get the same error, is there anyway i can make this a string so that it’ll work

any guidance is appreciated

thanks

format is capable of handling numbers without explicit type conversion. However the join method is not, it requires all elements in the iterable that’s joined to be a string.

hi sanity, i didnt bother with the first part of the joinas i thought it was ok, cast both parts to str and looks to be working.

       .format(" ".join(str([cell.value for cell in cellsOfFoundRow])),
                  str(cellsOfFoundRow[3].value)))

thanks

I was thinking of something more like:

[str(number) for number in numbers]

After that it will be possible to correctly join elements using chosen separator.

1 Like

Hi Sanity thanks for check the code again, i changed to

 .format(" ".join([str(cell.value) for cell in cellsOfFoundRow]),
                  str(cellsOfFoundRow[3].value)))

i’m trying to pick up cleaner coding using python, so thanks for your time

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.