python - Python - In a text file, strip a line containing a number greater than a certain value
问题描述
I have the following code:
with open(rawfile) as f, open(outfile,'w') as f2:
for x in f:
if (':') not in x and ('Station') not in x and('--')not in x and('hPa') not in x:
f2.write(x.strip()+'\n')
The "...if ___ not in x..." lines identify a line containing that string and removes the line while keeping the rest of the text in the same format. I would like to do this same thing, but remove any line containing a number greater than 10000.
解决方案
您应该可以通过合并正则表达式来做到这一点(因为您拥有的是一个字符串)。为此,你可以做类似的事情
import re
re.findall(r'\d{5,}', str)
这将识别 5 位或更多位的数字。将此包含在某种 if 子句中,以删除您希望消失的数字。
如果您想识别包括 5 位或更多数字在内的整行,您可以使用
re.findall(r'^.+(\d{5,}).+$', str)
推荐阅读
- ns-3 - 在 ns-3 模拟器中计算 point2point 链路上的节点能耗
- sql-server - 在 SQL 中使用多列计算重复项
- spring - 具有火花提交集群模式的春季启动
- php - 同时在laravel中保存多个请求
- r - R中带有摘要身份验证的POST请求
- php - SQLSTATE [23000]:违反完整性约束:1048 列 'color_code' 不能为空
- python - 我的不和谐机器人正在响应 dms(Python) 中的命令
- eclipselink - 如何使用 EclipseLink JPA 配置 HikariCP
- image - 如何获取 RGB-D 图像的顶层(用于位置检测)
- mysql - SQL获取计数分组的平均值