2024-09-30 01:20:02 发布
网友
当我使用API获取数据时,有些值有额外的空格。你知道吗
例如:Z Miramar Wellington Nz
Z Miramar Wellington Nz
如何纠正?你知道吗
import re # Using a regular expression, replace any sequence of spaces with a single space txt = re.sub(' +',' ', txt)
您可以使用^{}:
>>> import re >>> s = 'Z Miramar Wellington Nz' >>> re.sub(r'\s+', ' ', s) 'Z Miramar Wellington Nz'
或^{}后跟^{}:
>>> ' '.join(s.split()) 'Z Miramar Wellington Nz'
把绳子分开,然后再连接起来。像这样的
strText = ' '.join(strText.split())
您可以使用^{} :
或^{} 后跟^{} :
把绳子分开,然后再连接起来。像这样的
相关问题 更多 >
编程相关推荐