Python 使用numpy时,逐行读取并在条件下跳过

Python 使用numpy时,逐行读取并在条件下跳过,python,python-3.x,numpy,Python,Python 3.x,Numpy,我试图定义一个打开txt文件的函数,使其成为N*3矩阵。但是,应跳过与某些条件不匹配的行,并在继续读取这些行之前显示错误消息 这是我的代码: import numpy as np def dataLoad(filename): data=np.loadtxt(filename) return data 所以我有矩阵,但现在我想逐行阅读它,在不满足条件时跳过,显示一条错误消息,解释错误是什么,发生在哪一行,然后继续 条件是: •第一列必须是介于10和60之间的数字。 •第二列必须

我试图定义一个打开txt文件的函数,使其成为
N*3
矩阵。但是,应跳过与某些条件不匹配的行,并在继续读取这些行之前显示错误消息

这是我的代码:

import numpy as np
def dataLoad(filename):
    data=np.loadtxt(filename)
    return data
所以我有矩阵,但现在我想逐行阅读它,在不满足条件时跳过,显示一条错误消息,解释错误是什么,发生在哪一行,然后继续

条件是:
•第一列必须是介于10和60之间的数字。
•第二列必须为正数。
•第三列必须为1、2、3或4

编辑 我试过:

import numpy as np
def dataLoad(filename):
    data=np.loadtxt(filename)
    for row in data:
        if (row[0] < 10) or (row[0] > 60):
            print("Temperature is out of range")
            continue
        elif (row[1]<0):
            print("Growth rate is negative")
            continue
        elif (row[2]!=1) or (row[2]!=2) or (row[2]!=3) or (row[2]!=4):
            print("Bacteria is not 1, 2, 3 or 4")
            continue
    return data

将numpy导入为np
def数据加载(文件名):
data=np.loadtxt(文件名)
对于数据中的行:
如果(第[0]行<10)或(第[0]行>60):
打印(“温度超出范围”)
持续
elif(第[1]60行):
打印(“温度超出范围”)

elif(第[1]行我想这是您需要的代码。如果我有任何错误,请告诉我

import numpy as np

def dataLoad(filename):
    #opening and reading the data file
    file=open(filename,'r').readline()
    #creating the numpy matrix object
    data=np.matrix(file)

    print('raw data:',data)

    #checking the conditions
    #... for first column
    i=0
    while(i<len(data)):
        if data.item(i,0)<10 or 60<data.item(i,0):
            print('error message')
            print('removed',data[i])
            data=np.delete(data,(i),axis=0)
        else:
            i+=1

    print('data after checking 1st condition:',data)

    #... for second column
    i=0
    while(i<len(data)):
        if data.item(i,1)<0:
            print('error message')
            print('removed',data[i])
            data=np.delete(data,(i),axis=0)
        else:
            i+=1

    print('data after checking the 2nd condition:',data)

    #... for third column
    i=0
    while(i<len(data)):
        if data.item(i,2) not in (1,2,3,4):
            print('error message')
            print('removed',data[i])
            data=np.delete(data,(i),axis=0)
        else:
            i+=1

    print('data after checking the 3rd condition:',data)
    return data

print(dataLoad('test.txt'))
将numpy导入为np
def数据加载(文件名):
#打开并读取数据文件
file=open(文件名为'r')。readline()
#创建numpy矩阵对象
data=np.matrix(文件)
打印('原始数据:',数据)
#检查情况
#…第一列
i=0

而(i我认为您的问题在于,一旦满足条件,您就不会对数据进行变异。下面的代码应该可以帮助您解决问题:

import numpy as np
def dataLoad(filename):
    data=np.loadtxt(filename)
    retdata = []
    for row in data:
        if (row[0] < 10) or (row[0] > 60):
            print("Temperature is out of range")
            continue
        elif (row[1]<0):
            print("Growth rate is negative")
            continue
        elif (row[2]!=1) or (row[2]!=2) or (row[2]!=3) or (row[2]!=4):
            print("Bacteria is not 1, 2, 3 or 4")
            continue
    retdata.append(row)
    return retdata
将numpy导入为np
def数据加载(文件名):
data=np.loadtxt(文件名)
retdata=[]
对于数据中的行:
如果(第[0]行<10)或(第[0]行>60):
打印(“温度超出范围”)
持续
elif(行[1]又是我:)此代码是问题中编辑2的固定版本描述:

data = open("test.txt", "r").readlines()
for raw_line in range(len(data)):
    line = [int(n) for n in data[raw_line].split(' ')]# this splits the "1 2 3" to ['1','2','3'] and then, makes them integer([1,2,3])
    if (line[0] < 10) or (line[0] > 60):
        print("Temperature is out of range in row",raw_line)
    elif (line[1]<0):
        print("Growth rate is negative in row",raw_line)
    elif (line[2]!=1) or (line[2]!=2) or (line[2]!=3) or (line[2]!=4):
        print("Bacteria is not 1, 2, 3 or 4 in row",raw_line) 
data=open(“test.txt”、“r”).readlines()
对于范围内的原始线(len(data)):
line=[int(n)表示数据中的n[raw_line].split('')#这将“1,2,3”拆分为['1','2','3',然后使它们成为整数([1,2,3])
如果(第[0]<10行)或(第[0]>60行):
打印(“行中的温度超出范围”,原始行)

elif(第[1]行)如果你有条件,你就有了矩阵。是什么阻止你检查每一行是否满足这些条件?你可以迭代矩阵中的行并检查所有内容,而不是加载整行文本。如果只有你的条件满足,则迭代每一行并添加行。我尝试过这样做:将numpy作为np def dataLoad导入(文件名):数据中的行的data=np.loadtxt(文件名):如果(行[0]<10)或(行[0]>60):打印(“温度超出范围”)在编辑
data=np.loadtxt(文件名)时继续elif(行[1])
在中读取整个文件,这就是您获取所有行的原因。错误消息将在之后打印(打印它们不会删除任何内容)。感谢您的评论。我如何才能仅读取行?我认为有类似于“以f形式打开数据”的内容但我无法用与我的案例足够相似的示例来应用它。感谢您的输入。当我运行此代码时,我得到以下内容:dataLoad if data.item(1,I)中的文件“”,第15行@Maylis不客气。我刚刚编辑并测试了新代码。这应该可以。如果不行,请告诉我。@Maylis我只是阅读了编辑问题。在以前的版本中,无效行不会被删除。在这个版本中,无效行是。请告诉我此代码是否有效:)现在打印“错误消息[]”。第一个条件包含错误,如果第一列的值小于10或大于60,则应删除行。我更正了此问题,现在它打印[[39.122 0.81021 1.][[39.122 0.81021 1.]],但仍然太小…谢谢!@Maylis hi。欢迎您。我为“小于10或大于60”部分。但从您的评论中我了解到,此代码仍然存在一些问题。您能详细解释一下吗?(我的意思是,请给我一个test.txt行,该行已从数据中错误删除或未删除,但应该删除)谢谢你的回答。但是,当我运行代码时,它只打印错误消息,不打印正确的行。我尝试用“通过”替换“继续”,但它给出了相同的结果。你知道吗?
import numpy as np
def dataLoad(filename):
    data=np.loadtxt(filename)
    retdata = []
    for row in data:
        if (row[0] < 10) or (row[0] > 60):
            print("Temperature is out of range")
            continue
        elif (row[1]<0):
            print("Growth rate is negative")
            continue
        elif (row[2]!=1) or (row[2]!=2) or (row[2]!=3) or (row[2]!=4):
            print("Bacteria is not 1, 2, 3 or 4")
            continue
    retdata.append(row)
    return retdata
data = open("test.txt", "r").readlines()
for raw_line in range(len(data)):
    line = [int(n) for n in data[raw_line].split(' ')]# this splits the "1 2 3" to ['1','2','3'] and then, makes them integer([1,2,3])
    if (line[0] < 10) or (line[0] > 60):
        print("Temperature is out of range in row",raw_line)
    elif (line[1]<0):
        print("Growth rate is negative in row",raw_line)
    elif (line[2]!=1) or (line[2]!=2) or (line[2]!=3) or (line[2]!=4):
        print("Bacteria is not 1, 2, 3 or 4 in row",raw_line)