eamon1234
eamon1234

Reputation: 1585

Python unicode issue when iterating over list

I have an Excel workbook which I want to scroll through the contents of the first column and write each unique value to a new workbook i.e. no duplicates.

This code, without checking for duplicates, works fine:

def get_data_from_sheet(sheet_index, start_row, end_row, count):
for row in range(start_row, end_row):
    cell = sheet.cell(row,0)
    count = count+1 
    if row != start_row:
        sheet1.write(count,0,cell.value)
return count

However when I try to add a condition to check for duplicates, nothing is written to the Excel file:

from django.utils.encoding import smart_str

def get_data_from_sheet(sheet_index, start_row, end_row, count):
for row in range(start_row, end_row):
    cell = sheet.cell(row,0)
    count = count+1 
    if row != start_row:
        list.append(smart_str(cell.value))
        if smart_str(cell.value) not in list:
            sheet1.write(count,0,cell.value)
return count

Which puzzles me, because the list contains the values i.e.:

enter image description here

So what am I doing wrong? Thanks!

Upvotes: 0

Views: 213

Answers (1)

Zeugma
Zeugma

Reputation: 32095

By calling this:

    list.append(smart_str(cell.value))

before this:

    if smart_str(cell.value) not in list:

You are sure that the test will be always False, as you append the value to the list just before testing it is not in it. You have an issue with the logic here.

NB: avoid calling your variables with built-in types, call your list object lst or anything else making sense but don't use list as a variable.

Upvotes: 2

Related Questions