所以我创建了一个字符串,如下所示;
basic_punc = ' n.,/?!:;()[]{}-~&"'—'
我试图使用转义符在字符串中包含撇号,因此在我的代码中:
if (not char.isalnum()) & (not char in basic_punc):
# Do the rest of my function here....
其中char是我正在查看的字符。然而,我的代码似乎无法识别撇号是否属于我的basic_punc。我尝试过用双引号将字符串括起来,去掉转义符,但我的函数仍然认为撇号不是我创建的字符串的一部分。我能做什么?我正在使用jupyter笔记本,如果这是相关的。
更新:所以我一直在打印我一直在使用的词典,下面是我的其余功能:
if (not char.isalnum()) & (not char in basic_punc):
# Look up if I have seen this character before!
print(hidden)
print(char)
if not char in hidden:
hidden[char] = index
index += 1
print("'" in hidden)
# Find the second hidden character
end = line.find(char, i+1)
word = line[i+1:end]
sentences[hidden.get(char)].append(word)
line = line[end+1:]
i = end
我的隐藏打印输出为:{"<":0,"$":1,"#":2,":3}。它说撇号不在我的字典里,但这个字符是什么?
您的程序中一定有其他错误,原因是:
>>basic_punc = ' n.,/?!:;()[]{}-~&"'—'
>>''' in basic_punc
True
>>"'" in basic_punc
True