dimanche 23 octobre 2016

Writing XLS, different results from the “IF” and “ELSE” logic

Good day.

I want to replace the first words of the cell contents in an excel spread sheet, if they exist, then write them to a new xls file.

However when it’s run, it doesn’t replace all the words.

When the code part “ELSE” removed, it changes the target contents, but ignore writing some other cells.

enter image description here

The code:

merged_file_name = "C:\\new file.xls"

dest_book = xlsxwriter.Workbook(merged_file_name)
dest_sheet_1 = dest_book.add_worksheet()
dest_row = 0
temp = 0

temp_book = xlrd.open_workbook("C:\\old file.xls")
temp_sheet = temp_book.sheet_by_index(0)

the_list = ["general", "usual", "always"]

for row_index in range(0, temp_sheet.nrows):
    a_cell = temp_sheet.cell_value(row_index, 0)

    for each in the_list:

        if each == a_cell[0:a_cell.find(" ")]:

            dest_sheet_1.write(row_index, 0, a_cell.replace(each + " ", each + "-"))

        else:
            dest_sheet_1.write(row_index, 0, a_cell)


dest_book.close()

Could you please help me, what went wrong and how can I correct it?

Thanks.

Aucun commentaire:

Enregistrer un commentaire