python - 我正在尝试在字符串中包含撇号,以便可以将其作为文本文件中的标点符号忽略
问题描述
所以我正在创建一个字符串,如下所示;
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
我的隐藏打印输出是:{'<': 0, '$': 1, '#': 2, ''': 3}。它说撇号不在我的字典中,但是这个字符是什么?
解决方案
您一定在程序中犯了一些其他错误,原因是:
>>basic_punc = ' \n.,/?!:;()[]{}-~&\"\'—'
>>'\'' in basic_punc
True
>>"'" in basic_punc
True
推荐阅读
- r - 通过 grImport2 绘制 svg 在终端中可以正常工作,而不是通过 knitr
- c++11 - 通过值捕获本地指针,其余的在 lambda 中通过引用捕获
- javascript - 为什么我的 jqRouter.renderViewSuccess 事件处理程序被调用了两次?
- excel - 根据单元格值隐藏或取消隐藏工作表
- android - Android recyclerview defauilitemanimator 更改持续时间
- dropbox - 将 Google 照片或 DropBox 中的图像列表导出为 CSV
- reactjs - React Navigation V2 不显示栏,React Native
- python - 无法分配“9”:“Characterweapons.weaponid”必须是“Weapons”实例
- php - Laravel(5.6)最后一条消息的雄辩顺序
- r - 使用 purrr 迭代时间序列数据