如何解决我试图在字符串中包含一个撇号,这样我就可以将它作为文本文件中的标点符号忽略
所以我创建一个字符串如下;
basic_punc = ' \n.,/?!:;()[]{}-~&\"\'—'
我正在尝试使用转义字符在我的字符串中包含撇号,以便在我的代码中:
if (not char.isalnum()) & (not char in basic_punc):
# Do the rest of my function here....
其中 char 是我正在查看的字符。但是,我的代码似乎没有将撇号识别为属于我的 basic_punc。我尝试将字符串括在双引号中,删除转义字符,但我的函数继续将撇号视为不是我创建的字符串的一部分。我能做什么?如果相关的话,我正在使用 jupyter notebook。
更新:所以我一直在打印我一直在使用的字典,这是我的其余功能:
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
我用于打印隐藏的输出是: {'
解决方法
您一定在程序中犯了一些其他错误,原因:
>>basic_punc = ' \n.,/?!:;()[]{}-~&\"\'—'
>>'\'' in basic_punc
True
>>"'" in basic_punc
True
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。