如何使用python删除中文字符串中不连续的空白字符

2025-03-22 01:16:22
推荐回答(1个)
回答1:

# 用正则
import re
str1 = 'hello wor ld \r\n'
str2 = re.sub(r'\s', '', str1) 

# 正则'\s' 表示空白字符, 包括空格、\r\n\t等..

print str2  # 输出 helloworld