如何解决从字符串中删除标点符号
我想知道如何从以下列表中删除一些标点符号
string.punctuation
Out: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
具体地说,我想删除@?&#!^_
以便在这里使用:
def pr(text):
#1 Remove Punctuationa
nopunc = [char for char in text if char not in string.punctuation]
nopunc = ''.join(nopunc)
#2 Remove Stop Words
clean = [word for word in nopunc.split() if word.lower() not in stopwords.words('english')]
return clean
预先感谢您的回答和建议。
解决方法
您可以使用re.sub
re.sub("[@?&#!^_]","",string.punctuation)
'"$%\'()*+,-./:;<=>[\\]`{|}~'
,
与从输入字符串中删除标点符号的方式相同。
<head>
<link rel="stylesheet" href="style.css" />
<script
src="https://kit.fontawesome.com/71fb624cb4.js"
crossorigin="anonymous"
></script>
<link
href="https://fonts.googleapis.com/css2?family=Lato&display=swap"
rel="stylesheet"
/>
<script src="js.js"></script>
</head>
<body>
<div class="container">
<a class="home h" id="h" onclick="changeOpacity(this)"><i class="fas fa-home"></i>Home</a>
<a class="home acc"><i class="far fa-user-circle"></i></i>Account</a>
<a class="home fli"><i class="fas fa-fighter-jet"></i>Flights</a>
</div>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。