[英]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.