我需要在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
答案 0 :(得分:2)
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
答案 1 :(得分:1)
嗯,你可以做第二次,非空白如下:
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中使用(在那里看起来像一组奇怪的值)。
答案 2 :(得分:1)
使用辅助生成器:
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):
...