1

I am trying to write a script that will delete rows based on whether or not the row's corresponding cell value in the first column contains a specific character, in this case a '#'.

I have tried writing the following function that would hopefully iterate through the sheet and return a new sheet with the deleted rows. LabSample is the sheet that I am working in.

def remove_headers():
    for rowNum in range(2, LabSample.max_row):
        sys_sample_code = LabSample.cell(row=rowNum, column=1).value
        if '#' not in sys_sample_code:
            continue
        else:
            LabSample.delete_rows(rowNum, 1)
    return LabSample

for row in LabSample.rows:
    print(row[0].value)

I currently am not getting any error messages, but the output I am getting is unchanged from the input. It appears that no rows have been deleted.

ZzZylo
  • 31
  • 5

2 Answers2

0

Try casting sys_sample_code as a str()

#...
sys_sample_code = LabSample.cell(row=rowNum, column=1).value
        if '#' not in str(sys_sample_code): # SEE EDIT HERE
        # ...
FilteredFrames
  • 182
  • 2
  • 8
0

I'd probably do this through importing the table in numpy and then index the rows which contain the '#', and finally use np.delete to remove the rows before pushing it back to where it came.

Here's a short example which kinda demonstrates what I'm talking about. just replace the starting array 'x' with your data array and analyze along the column you're interested in.

import numpy as np

x = np.array(
        [1,2,5,'a','#test1', 'b', 7, '#test2', 9]
         )

index = {count: pos for count, pos in enumerate(x) if '#' in pos[:]}

x = np.delete(x, list(index.keys()))


In [17]: x
Out[17]: array(['1', '2', '5', 'a', 'b', '7', '9'], dtype='<U11')

Michael Green
  • 719
  • 6
  • 15
  • If you share your data, or an example data file, we could be more specific with the syntax, but for now this general example should point you in the right direction. – Michael Green Nov 01 '19 at 22:16