Search code examples
pythondictionaryspyderxlrdxlwt

Python dict.keys() has the key but acts as it doesn´t have it


I´m replacing a lot of values in an Excel, so I have a dictionary with hundreds of keys, but when a key has a slash ( / ), it doesn´t enter the if.

As you can see I have tried using doble, //, and adding r before to make it a literal.

When I do is ("stringwith /slash") in dict.keys() I get a True, but when I run the program, they are skipped, as if they don´t exist.

import xlwt
import xlrd


dictForReplacement={}


for i in range(0,101):
    x = "Estigfend" + str(i) + str(i)+ " puntos de 100"
    dictForReplacement[x] = str(i)
    x = "Estigfend" + str(i) + str(i)+ " puntos de 100Completada con retraso"
    dictForReplacement[x] = str(i)
    x = "Estigfend" + str(i) + str(i)+ " puntos de 100Presentada de nuevo"
    dictForReplacement[x] = str(i)
    x = "Estigfend" + str(i) + str(i)+ " puntos de 100Borrador"
    dictForReplacement[x] = str(i)    
    x = "Estigfend" + str(i) + str(i)+ " puntos de 100Sin entregar"
    dictForReplacement[x] = str(i)    
    x = "Estigfend" + str(i)+"/100" + str(i)+ " puntos de 100Sin entregar"
    dictForReplacement[x] = str(i)    
    x = "Estigfend" + str(i)+"/100" + str(i)+ " puntos de 100"
    dictForReplacement[x] = str(i)  

dictForReplacement[r"EstigfendSin entregar"] = "NP"



dictForReplacement["EstigfendTarea asignada"] = "TA"


#here is the problem, the number was in case I founded the solution

dictForReplacement[r"Estigfend /100Sin calificación"] = "NP 00"
dictForReplacement[r"Estigfend /100Sin calificación"] = "NC 0"
dictForReplacement["Estigfend //100Sin calificación"] = "NC 1"
dictForReplacement[r'Estigfend //100Sin calificaciónCompletada con retraso'] = "NC 2"
dictForReplacement['Estigfend /100Sin calificaciónCompletada con retraso'] = "NC 3"
dictForReplacement[r"Estigfend /100Sin calificaciónCompletada con retraso"] = "NC 4"
dictForReplacement[r"Estigfend /100Sin calificaciónBorrador"] = "NC 5"



dictForReplacement["Estigfend /100Sin calificación"] = "NP" 

###################################################
#heres the if


ExcelNewName = "AAA"
########################################## Name of Excel to be modified
workbook = xlrd.open_workbook('Intento Calificaciones clear.xlsx')
sheet = workbook.sheet_by_name('Hoja1')

#write the data into new Excel file:
new_workbook = xlwt.Workbook()
new_sheet = new_workbook.add_sheet('Hoja1')


for i in range(sheet.nrows):
    
    data = [sheet.cell_value(i, col) for col in range(sheet.ncols)]

    for index, value in enumerate(data):

        if value in dictForReplacement.keys():
            new_sheet.write(i, index, str(dictForReplacement.get(value)))
        else:
            new_sheet.write(i, index, value)
            
            
new_workbook.save(ExcelNewName+'.xls')

This is my code without Ktinder, since its a GUI I know it isnt needed here

So now you can also run it.

edit4: the next 3 lines wrong Idk how to add a excel file here, the biggest problem is the strings with /, so just create a page and have a cell be : Estigfend /100Sin calificación

edit2: someone said there must likely be that excel is adding other things, I´ll look into it

edit3: add a print to see value and error, same string


Solution

  • It was invisible characters!

    My mistake, dict.keys() works fine obviously, but when printing the values that didn´t had a key (when I knew I had all possible options as keys), I saw they looked just like my keys, but they aren´t the same due to invisible characters!!!

    I changed part of the code to get a list of all the bad values, using the else, then in a for of this list applied print(repr(value)) to see the invisible characters

    and saw this:

    Result of print(repr(value)) using bad values

    So my Excel is weird, as previously said by @CollinHeist in comments, this did it

    dictForReplacement[r"Estigfend\xa0/100Sin calificación"] = "NP"
    

    Thanks commenters