简体   繁体   中英

Find lines starting with a number in range

I've got data that looks like this

18  -3.248418   0.326600    1.096954    0.536294    -0.192884   0.071945    1.000000    
19  -3.498030   0.396155    1.101706    0.628693    -0.231044   0.071432    1.000000    
20  -3.827249   0.453596    1.135211    0.723203    -0.312638   0.077363    1.000000    
21  -4.178354   0.558543    1.094458    0.813547    -0.383654   0.059875    1.000000    
22  -4.520537   0.712326    1.025310    0.918873    -0.494836   0.040987    1.000000    
23  -4.898419   0.874629    0.872843    1.072562    -0.581968   0.022534    1.000000    
24  -5.178654   0.986722    0.762628    1.235058    -0.656758   0.022830    1.000000    
25  -5.497410   1.139729    0.608902    1.419559    -0.751435   0.012771    1.000000    
26  -5.685015   1.279948    0.377152    1.601394    -0.869300   0.016318    1.000000    
27  -6.058228   1.424318    0.105117    1.845609    -1.011224   0.009828    1.000000    
28  -6.426589   1.548294    -0.172656   2.048423    -1.112269   0.015640    1.000000    
29  -6.786007   1.633135    -0.527714   2.268596    -1.259513   0.002945    1.000000    

How do I get only lines starting with a number in a specific range?

I want to do something like if line.startswith("20-25"): but that obviously doesn't work. I need to do this for multiple files with different ranges.

Use the condition 20 <= num <= 25 , where num is an integer.

import csv
with open('filename') as f:
    reader = csv.reader(f, delimiter=' ')
    for row in reader:
       if  20 <= int(row[0]) <= 25:
            #Do something here 

You could try:

if int(line[:2]) in range(20, 26):

If your numbers may vary in length (ie not always two characters), you will need to split the line:

if int(line.split(" ", 1)[0]) in range(20, 26):

Or, given that you will probably need to process the rest of the line as floats under some circumstances:

index, data = line.split(" ", 1)
if int(index) in range(20, 26):
    data = map(float, data.split(" "))

Because you're looking for one range, you can stop reading the file once you've hit the end of that range. And because your columns are separated by whitespace, str.split() should do the trick:

def process_file(filename, range_low, range_high):
    with open(filename) as f:
        for line in f:
            row = line.split()
            i = int(row[0])
            if i < range_low:
                continue
            if i > range_high:
                break
            data = [float(x) for x in row[1:]]
            # ...

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