python - 如何替换字符串中的空格逗号?
问题描述
我有以下字符串,我想将其解析为单词。这是字符串语句
text = "If it is hot , don’t touch"
到目前为止我已经尝试过:
import string
text = "If it is hot , don’t touch"
words = [word.replace(',', '') for word in text.split()]
print(words)
但是我得到了以下结果:
['If', 'it', 'is', 'hot', '', 'don’t', 'touch']
我想要的结果是:
['If', 'it', 'is', 'hot', 'don’t', 'touch']
解决方案
text = "If it is hot , don’t touch"
newtext = text.replace(",", "")
words = newtext.split()
print(words)
推荐阅读
- ios - 如何使用另一个 iWatch 应用程序以编程方式启动 Breathe 应用程序?
- python - 我想在 python 数据框合并时添加一个条件
- glassfish - 从 Web 控制台添加资源时出现 Glassfish 5.1.0 错误
- azure - 我可以使用 Azure Active Directory 来保存我的应用程序的用户存储吗?
- android - 如何正确验证蓝牙 MAC 地址?
- flutter - 在 BottomNavigationBarItem 图标上添加文本小部件
- android - 在 Ionic 3 的应用内购买 2 中列出产品
- c# - 使用表达式的 MongDb 投影查询不适用于 C#
- c# - CLoudBLob 存储提取属性(404 错误)
- php - 如何在php中按值对多维数组进行排序