Python dict.keys具有密钥,但是却没有密钥

问题描述

我要在Excel中替换很多值,因此我有一个包含数百个键的字典,但是当键带有斜杠(/)时,它不会输入if。

如您所见,我已经尝试过使用doble,//,并在前面加上r使其成为文字

当我在dict.keys()中使用(“ stringwith / slash”)时,会得到True,但是当我运行该程序时,它们会被跳过,就像它们不存在一样。

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,value)
            
            
new_workbook.save(ExcelNewName+'.xls')

这是我没有Ktinder的代码,因为它是一个GUI,我知道这里不需要它

所以现在您也可以运行它。

edit4:接下来的3行错误 Idk如何在此处添加excel文件,最大的问题是带有/的字符串,因此只需创建页面并具有一个单元格即可: Estigfend / 100Sin证书

edit2:有人说excel可能添加了其他内容,我会调查一下

edit3:

add a print to see value and error,same string

解决方法

那是看不见的角色!

我的错误dict.keys()显然可以正常工作,但是当打印没有键的值时(当我知道我可以将所有可能的选项作为键时),我看到它们看起来就像我的键一样,但是由于看不见的字符,它们是不一样的!!

我更改了代码的一部分,使用else获取所有错误值的列表,然后在该列表的for中应用print(repr(value))来查看不可见的字符

看到了:

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

所以我的Excel很奇怪,就像@CollinHeist在评论中所说的那样,做到了

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

感谢评论者