我正在使用python消除文本中的空格
rooms
expected result =(“ python”,“ is”,“ the”,“ best”)或“ python is best”
答案 0 :(得分:1)
您可以使用re
模块:
import re
test_text="Python is the best"
output = re.sub(r'(\s){2,}', r'\1', test_text)
print(output)
打印:
Python is the best
编辑(不带re模块):
test_text="Python is the best"
print(test_text.split())
打印:
['Python', 'is', 'the', 'best']
编辑2:
#to join it to one string:
print(' '.join(test_text.split()))
打印:
Python is the best