numpy.loadtxt跳过多行 [英] numpy.loadtxt Skipping multiple rows

查看:1119
本文介绍了numpy.loadtxt跳过多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信该主题的标题可以解释我所寻找的内容.我很想知道跳过多行的语法是什么;我似乎在任何地方都找不到这样的信息.

I believe the title of this thread explains what I am looking for. I am curious to know what the syntax is for skipping multiple rows; I can't seem to find such information anywhere.

推荐答案

使用help(np.loadtxt).您会发现skiprows参数将允许您跳过前N行:

Use help(np.loadtxt). You'll find the skiprows parameter will allow you to skip the first N rows:

In [1]: import numpy as np

In [2]: help(np.loadtxt)
Help on function loadtxt in module numpy.lib.npyio:

loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
    ...
    skiprows : int, optional
        Skip the first `skiprows` lines; default: 0.

因此,您要跳过N

np.loadtxt(fname, skiprows=N)


如果您需要过滤除 first N行之外的其他行,请使用np.genfromtxt,它可以使用一个生成字符串的迭代器作为其第一个参数:


If you need to filter rows other than the first N rows, use np.genfromtxt which can take an iterator which yields strings as its first argument:

with open(filename, 'r') as f:
    lines = (line for line in f if predicate(line))
    arr = np.genfromtxt(lines)


要跳过中间的一系列行,例如第47--50行,可以使用itertools这样:


To skip a sequence of rows in the middle, such as rows 47--50, you could use itertools like this:

import itertools as IT

with open(filename, 'r') as f:
    lines = IT.chain(IT.islice(f, 46), IT.islice(f, 4, None))
    arr = np.genfromtxt(lines)

这篇关于numpy.loadtxt跳过多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆