使用标点符号列表进行re.sub
有没有办法列出标点符号,并将其放入re.sub中。我正在使用F-string文字,但逃生角色打破了一切。我应该手动键入它吗?
marks = [',', '。', '—', '《', '》', '□', '●', '/', '{', '}', '·', '、', '「', '」','|']
punctuation = '|'.join(['\\' + f'{n}' for n in marks])
re.sub('\ |\?|\.|\!|\/|\;|\:', '', 'line')
Is there a way to take a list of punctuation, and put it into a re.sub. I was using f-string literals but the escape character breaks everything. Should I just type it manually?
marks = [',', '。', '—', '《', '》', '□', '●', '/', '{', '}', '·', '、', '「', '」','|']
punctuation = '|'.join(['\\' + f'{n}' for n in marks])
re.sub('\ |\?|\.|\!|\/|\;|\:', '', 'line')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管我更喜欢使用角色类,但您的工作没有错。无法从您的帖子中100%确定,但听起来您正在尝试从字符串中删除某些标点符号?例子:
Nothing wrong with what you're doing, though I prefer to use a character class. Can't be 100% sure from your post but it sounds like you're trying to remove certain punctuation from a string? Example: