簡體   English   中英

從python中的文件讀取第二行的最短方法

[英]Shortest way to read every second row from file in python

我需要從我的ascii文件的第二行創建數組。 從python文件中讀取第二個非空行的最短方法是什么? 也許通過numpy的genfromtxt?

文件示例:

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

另一種方法(在Python2上,如果文件很大,則可能要使用izip

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

好吧,您可以每隔2次進行非空白操作,如下所示:

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

但不確定如何從這些行中提取數據以供numpy使用(看起來像那里的一組奇怪的值)。

使用輔助生成器:

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

現在,您可以使用類似以下內容瀏覽文件

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM