Python - 在文本文件中,去除包含大于特定值的数字的行

Python - In a text file, strip a line containing a number greater than a certain value

我有以下代码:

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')

“...if ___ not in x...”行标识包含该字符串的行并删除该行,同时将其余文本保持为相同格式。我想做同样的事情,但删除任何包含大于 10000 的数字的行。

您应该可以通过合并正则表达式来做到这一点(因为您拥有的是一个字符串)。为此,你可以做类似

的事情
import re    

re.findall(r'\d{5,}', str)

这将识别具有 5 个或更多数字的数字。将其包含在某种 if 子句中以删除您想要的数字。

如果您想要识别包含 5 位或更多数字的整行,您可以使用

re.findall(r'^.+(\d{5,}).+$', str)

最简单的是使用regexp和分组:

match = re.match(r'regexpToIdentyMyNumber(\d+)', x)
my_number = float(match.group(1)))
if my_number > 10000:
    continue # do my thing

基本上,您需要定义一个模式来标识您的号码,然后使用括号声明并将号码(\d+)保存为一个组,然后可用于进行进一步的计算。