在 Python 中的空格上拆分字符串

新手上路,请多包涵

我正在寻找 Python 等价物

String str = "many   fancy word \nhello    \thi";
String whiteSpaceRegex = "\\s";
String[] words = str.split(whiteSpaceRegex);

["many", "fancy", "word", "hello", "hi"]

原文由 siamii 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 789
2 个回答

没有参数的 str.split() 方法在空格上拆分:

 >>> "many   fancy word \nhello    \thi".split()
['many', 'fancy', 'word', 'hello', 'hi']

原文由 Sven Marnach 发布,翻译遵循 CC BY-SA 4.0 许可协议

import re
s = "many   fancy word \nhello    \thi"
re.split('\s+', s)

原文由 Óscar López 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题