简体   繁体   中英

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.

You should be able to this by incorporating Regex (since what you have is a string). For that, you could do something like

import re    

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

This will identify numbers with 5 or more digits. Include this in some sort of if clause to remove the numbers you desire gone.

If you're wanting to identify the whole line including a 5 digit or more number, you can use

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

The easiest is to use regexp and grouping:

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

Basically you need to define a pattern that identifies your number and then use the parentheses to declare and save the number (the \\d+ ) as a group, which can then be used to do further calculations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM