简体   繁体   中英

Shortest way to read every second row from file in python

I need to create array from every second row in my ascii file. What is the shortest way to read every second nonempty row from the file in python? Maybe via numpy's genfromtxt?

File example:

hd105373_550  Alpha=12 08 36.33  Delta=+05 58 26.4  Mtime=02:04.8  Stime=12:21.3  Z=37.8  Focus=184.22
hd105373_550  Alpha=12 08 36.34  Delta=+05 58 25.7  Mtime=02:07.7  Stime=12:24.2  Z=37.8  Focus=184.22

hd105373_800  Alpha=12 08 36.34  Delta=+05 58 25.4  Mtime=02:10.1  Stime=12:26.6  Z=37.9  Focus=184.22
hd105373_800  Alpha=12 08 36.31  Delta=+05 58 25.0  Mtime=02:12.9  Stime=12:29.4  Z=37.9  Focus=184.22
with open('your_file') as fin:
    data = (i for i in fin if not i.isspace())
    for row in data:
        row = next(data)
        # ... do something with every second non empty row

Another way (On Python2 you might want to use izip if the file is large)

with open('your_file') as fin:
    for odd, even in zip(*[(i for i in fin if not i.isspace())]*2):
        # ... do something with even

Well, you can do every 2nd, non blank as the following:

from itertools import islice

with open('your_file') as fin:
    non_blank = (line for line in fin if line.strip())
    every2 = islice(non_blank, 1, None, 2)
    for row in every2:
        # do something with row

But not sure how then you extract the data from those lines for use in numpy (looks like a weird set of values in there).

Use a helper generator:

def only_every_second_nonempty(iterator):
    yield_next_line = False  # Set to True if lines 1, 3, 5... should be returned
    for value in iterator:
        if not value.strip(): continue  # Skip empty line
        if yield_next_line:
            yield value
        yield_next_line = not yield_next_line

Now you can go through the file with something like

with open('your_file') as f:
    for row in only_every_second_nonempty(f):
        ...

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