可能存在空值list类型空值去除方法
while "" in tmp_list:# 判断是否有空值在列表中
tmp_list.remove("")# 如果有就直接通过remove删除
print(tmp_list)
str类型空值去除方法
1、strip方法去掉字符串两边(开头和结尾)的空格
space_str = ’ Remove the space around the string ’
print(space_str.strip())
2、lstrip方法去掉字符串左边的空格
left_space_str = ’ Remove the space to the left of the string ’
print(left_space_str.lstrip())
3、rstrip方法去掉字符串右边的空格
right_space_str = ’ Remove the space to the right of the string ’
print(right_space_str.rstrip())
4、replace方法替换字符串的空格为空
all_space_str = ’ The space of the replacement string is empty ’
print(all_space_str.replace(’ ', ‘’)
注意:这里说一下replace方法的具体用法
str.replace(old_str, new_str, [max])
old_str:代表原来的字符串,new_str:代表替换之后的字符串,max:代表替换的次数
5、正则匹配替换空格
import re
all_str = " Regular matching without spaces "
print(re.sub(r"\s+", “”, all_str))
本文为学习笔记,仅是个人意见,仅供交流学习,请勿用于非法途径
转自https://blog.csdn.net/weixin_32340083/article/details/116183516