网站首页 > 技术文章 正文
在 Python 中,删除空白可以分为几种不同的情况,常见的是针对字符串或列表中空白字符的处理。
一、删除字符串中的空白
1. 删除字符串两端的空白(空格、\t、\n 等)
使用 .strip() 方法:
s = " Hello, world! "
clean_s = s.strip()
print(clean_s) # 输出:'Hello, world!'
- s.strip():去除首尾空白
- s.lstrip():去除左侧空白
- s.rstrip():去除右侧空白
2. 删除字符串中间的所有空白字符
使用 str.replace() 或正则表达式 re.sub():
方法一:replace()(只处理空格)
s = "Hello world"
clean_s = s.replace(" ", "")
print(clean_s) # 输出:'Helloworld'
方法二:使用 re.sub() 删除所有类型的空白字符
import re
s = "Hello\t world\nPython"
clean_s = re.sub(r"\s+", "", s)
print(clean_s) # 输出:'HelloworldPython'
说明:
- \s 表示所有空白字符(空格、制表符、换行符等)
- + 表示一个或多个
3. 删除字符串中连续空格保留一个
如果想将多个空格变成一个空格:
import re
s = "Hello world Python"
clean_s = re.sub(r"\s+", " ", s)
print(clean_s) # 输出:'Hello world Python'
二、删除列表中的空字符串或空白项
1. 删除列表中完全为空的项("" 或 " ")
lst = ["apple", "", "banana", " ", "cherry"]
clean_lst = [item for item in lst if item.strip()]
print(clean_lst) # 输出:['apple', 'banana', 'cherry']
说明:
- item.strip() 为 False 时表示空白项(包括空格、换行、制表符等)
三、删除文件中每行的空白(比如清理文本文件)
with open("example.txt", "r", encoding="utf-8") as f:
lines = f.readlines()
clean_lines = [line.strip() for line in lines if line.strip()]
with open("cleaned.txt", "w", encoding="utf-8") as f:
f.writelines(line + "\n" for line in clean_lines)
说明:
- line.strip() 去除首尾空白
- if line.strip() 避免空行写入
四、删除多维结构中的空白(如嵌套列表)
data = [["a", " "], ["", "b", "c"], [" ", ""]]
clean_data = [[item for item in sublist if item.strip()] for sublist in data]
print(clean_data) # 输出:[["a"], ["b", "c"], []]
总结:常用方法一览表
方法 | 说明 |
s.strip() | 删除字符串首尾空白 |
s.replace(" ", "") | 删除所有空格 |
re.sub(r"\s+", "") | 删除所有空白字符 |
s.split() + ' '.join() | 去除多余空格,保留单个空格 |
列表推导式 + strip() | 删除列表中空白项 |
猜你喜欢
- 2025-06-28 python文件操作常用方法整理(python文件操作总结)
- 2025-06-28 Python3查看文件是否存在,以及读、写与执行的属性
- 2025-06-28 Python 中的前缀删除操作全指南(python删除后缀)
- 2025-06-28 Python教程(18)——python文件操作详解
- 2025-06-28 你的手机是不是经常提示存储空间不足?Python帮你清理重复文件
- 2025-06-28 用于清理数据的 5 个简单有效 Python 脚本
- 2025-06-28 「万能Python」-15-文件处理(关于python对文件的处理)
- 2025-06-28 Linux系统自带Python2&yum的卸载及重装
- 2025-06-28 Linux 下海量文件删除方法效率对比,最慢的竟然是 rm
- 2025-06-28 python应用-shutil详解文件复制/移动/删除功能
- 272℃Python短文,Python中的嵌套条件语句(六)
- 271℃python笔记:for循环嵌套。end=""的作用,图形打印
- 269℃PythonNet:实现Python与.Net代码相互调用!
- 264℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 264℃Python实现字符串小写转大写并写入文件
- 123℃原来2025是完美的平方年,一起探索六种平方的算吧
- 104℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 99℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
-
- Python数据分析实战-dataframe分组提取每一组的首条记录
- 如何使用Python将多个excel文件数据快速汇总?
- 「Python数据分析」Pandas进阶,使用groupby分组聚合数据(二)
- 还在熬夜合并30个Excel 3个案例,带你用Python玩转Excel高阶操作
- python数据分析实战:pandas分组聚合-自定义聚合函数
- Python 知识点 #31 - 分组和聚(python分层聚类)
- 人生苦短,自学 python——pandas 的分组操作
- 利用Python进行数据分组/数据透视表
- 超实用!用Python快速实现数据分组统计与透视表
- Python 之 Pandas:数据分组聚合统计的魔法秘籍
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)