python-3.x - 如何从文本文件中删除仅数字行?
问题描述
假设我有一个文本文件,其中包含字母数字值和仅逐行长度为 10 位的数值,如下所示:
abcdefgh
0123456789
edf6543jewjew
9876543219
我想删除所有仅包含那些随机 10 位数字的行,即上述示例的预期输出如下:
abcdefgh
edf6543jewjew
在 Python 3.x 中如何做到这一点?
解决方案
with open("yourTextFile.txt", "r") as f:
lines = f.readlines()
with open("yourTextFile.txt", "w") as f:
for line in lines:
if not line.strip('\n').isnumeric():
f.write(line)
elif len(line.strip('\n')) != 10:
f.write(line)
推荐阅读
- java - Android 在 CircleImageView 中显示从相机捕获的图像
- algorithm - 查找添加到数组的运行总和时为 1 的最小数字
- botframework - 历史消息
- javascript - createReadStream 和 createWriteStream 无法正常工作
- python - Flask wtforms 嵌套变量
- python - 如何使用键列表复制字典
- spring-boot - SpringBoot:启动失败:字段
在 RestController 中需要一个 bean 类型 - css - 如何使用相同的 CSS 类应用不同的样式?
- snowflake-cloud-data-platform - 窗口函数与日期时间的结合
- python - 安装 discord.py 时,命令“python setup.py egg_info”在 /tmp/pip-build-_J7Ije/aiohttp/ 中失败,错误代码为 1