正在查找行的最大值,但错误:类型为“”的对象_csv.reader文件'没有len()

2024-06-18 11:45:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着将两个函数结合使用。基本上,我正在尝试导入一个csv文件,它是一系列具有我需要的值的行。然后我尝试使用第二个函数“findPeaks”来找到每行的最大值。我得到的错误是,我试图查看的行没有长度,但它们应该是值数组。我的错误是什么?谢谢你的帮助。你知道吗

def PSD(pulseData, pulseDataTraining):

     import csv;
     import numpy as np;
     import math
     photonTraining = 0; 

     with open(pulseDataTraining,'r') as csvfile:
         photonTraining = csv.reader(csvfile);
         print(photonTraining)
         for row in photonTraining:
             findPeaks(photonTraining,-30)  

     if photonTraining == 0:
         print("this is a string")

     return np.max(photonTraining)

第二个功能是:

def findPeaks(pulse, thresh):

     import numpy as np;
     import scipy as sp;

     peakVal = 0;
     for i in range(0,len(pulse)-1):
         while(pulse[i]<thresh):
             if np.abs(pulse[i]) < np.abs(pulse[i+1]):
                 peakVal = pulse[i+1];
             else:
                 peakVal = pulse[i];


     return peakVal;

Tags: csvcsvfile函数importnumpydefas错误
1条回答
网友
1楼 · 发布于 2024-06-18 11:45:36

更改为:

def PSD(pulseData, pulseDataTraining):
    with open(pulseDataTraining, 'r') as csvfile:
        # Create a csv.reader from Filehandle
        csvReader = csv.reader(csvfile)

        # Create a List of Peak Values per Row from CSV
        photonTraining_peak = \
            [findPeaks(photonTraining, -30) for photonTraining in csv.reader(csvfile)]

    print('row_max:{}'.format(photonTraining_peak),)
    return np.max(photonTraining_peak)

你的findPeaks(...将提高

TypeError: unorderable types: str() < int()

更改为

import csv
import numpy as np

def findPeaks(pulse, thresh):
    # pulse is Type List of String
    sorted_pulse = sorted(pulse, reverse=True)
    print(sorted_pulse)

    peakVal = 0
    for s in sorted_pulse:
        v = int(s)
        if v < thresh:
            if np.abs(peakVal) < np.abs(v):
                peakVal = v
        else:
            break
    return peakVal

用Python:3.4.2测试

相关问题 更多 >