跳过2D列表中的第一行并使用值

时间:2014-06-03 13:49:06

标签: python list csv compare

我正在开发一个项目,我有两个已转换为2D列表的csv文件。我有List1在某个区域内有一定数量的钻孔,而List2有相应的多个值来计算平均值,最小值和最大值。我遇到麻烦,因为我觉得这个功能是:

  1. 不跳过列表中值的标题,因此没有得到整数

  2. 可能没有比较这两个名单,因为我一直卡在第1步

  3. 到目前为止我所拥有的是:

    def height(bReader, dataList):
        print "Height function ran, but no result"
        # bReader is bores inside polygon
        # dataList BORE_DATA.csv as 2D list
        avgHeight = 0
        minN = 9e99
        maxPh = 0
        numCount = 0
        for row in bReader:
            for row2 in dataList:
                if row[0] == row2[0]:
                    if row2[1] == '':
                        if row2[1][0] == '<':
                            row2[1] = row2[1][:1]
                    avgHeight += row2[4]
                    numCount += 1
    
                    print row2[4]
                    # Checks the minimum values of Nitrogen
    
                    if row2[6] < minN:
                        minN = row2[6]
    
                     # Checks the maximum values of Phosphorous
    
                    if row2[5] < maxPh:
                        maxPh = row2[5]
    
    
                    if lastID != row2[0]:
                       # Checks for final value before moving to next bore
                        aveHeight = avgHeight / numCount
                        print row[0]
                        print "Average Water Level" + aveHeight
                        print "Minimum Nitrogen" + minN
                        print "Maximum Phosphorous" + maxPh
                        avgHeight = 0
                        minN = 9e99
                        maxPh = 0
                        numCount = 0
                    lastID = row[0]
    

    这是追溯错误:

    Traceback (most recent call last):
      File "C:\Users\Lounge\Desktop\V2\ReRun V2.py", line 39, in main
        height(levelsList, bdataList)
      File "C:\Users\Lounge\Desktop\V2\ReRun V2.py", line 263, in height
        for row in bReader:
    TypeError: 'function' object is not iterable
    

    示例数据:

    List1数据

    REFERENCE   EASTING NORTHING    TOC ELEVATION (m)
    61610628    384835  6489341         20.24
    

    List2数据

    BORE REF NR BORE NAME   SAMPLE DATE    water level (m)  TDSolids (mg/L) pH  N (mg/L)    P (mg/L)
    61610628    JP20B   23/06/2000              3.71        430      8.8        0.28        0.007
    61610628    JP20B   27/10/2000              3.18        610      7.2        1.3        0.019
    

2 个答案:

答案 0 :(得分:0)

我对程序进行了一些更改以使其运行。要考虑的要点是:

  • 任何要以数字方式进行比较的值都必须转换为适当的数据类型,因为如果从csv导入它们将默认为字符串;
  • 要获取maxPH,您需要检查&gt;不是&lt;
  • 我已经移动了内循环外的每个孔的摘要信息,这样就无需检查id是否已更改;
  • 我还初始化了外循环中的ave,max,min变量,以便为每个孔重置它们;
  • 根据您提供的数据,水位应为row2[3]而不是row2[4];
  • 我已经删除了检查if row2[1] == '':的逻辑逻辑,因为它似乎是多余的。

以下是修订后的代码:

def height(bReader, dataList):
    print("\nIn height function...")
    # bReader is bores inside polygon
    # dataList BORE_DATA.csv as 2D list
    for row in bReader:
        # initialise measurements for each bore
        avgHeight = 0
        minN = 9e99
        maxPh = 0
        numCount = 0

        for row2 in dataList:
            if row[0] == row2[0]:
                avgHeight += float(row2[3])
                numCount += 1

                print("Water level: {0}".format(row2[3])) # water level

                # Checks the minimum values of Nitrogen
                if float(row2[6]) < minN:
                    minN = float(row2[6])

                 # Checks the maximum values of Phosphorous
                if float(row2[5]) > maxPh:
                    maxPh = float(row2[5])

       # Checks for final value before moving to next bore
        aveHeight = avgHeight / numCount
        print row[0]
        print("Average Water Level: {0}".format(aveHeight))
        print("Minimum Nitrogen: {0}".format(minN))
        print("Maximum Phosphorous {0}".format(maxPh))
        avgHeight = 0
        minN = 9e99
        maxPh = 0
        numCount = 0


# added below for test
import csv

# create file objects and readers
f1, f2 = open('list1.csv', 'rb'), open('list2.csv', 'rb')
r1 = csv.reader(f1, delimiter=',', quotechar='"')
r2 = csv.reader(f2, delimiter=',', quotechar='"')

# create lists from readers
l1 = [[col for col in row] for row in r1]
l2 = [[col for col in row] for row in r2]

# check first couple of rows from lists
print('\nSample data from list 1\n' + '-'*23) 
for row in  l1[:2]: print(row)
print('\nSample data from list 2\n' + '-'*23) 
for row in l2[:3]: print(row)

# call function without headings from first rows
height(l1[1:], l2[1:])

产生以下输出:

Sample data from list 1
-----------------------
['REFERENCE', ' EASTING NORTHING', ' TOC ELEVATION (m)']
['61610628', ' 6489341', ' 20.24']

Sample data from list 2
-----------------------
['BORE REF', ' NR BORE NAME', ' SAMPLE DATE', ' water level (m)', ' TDSolids', '(mg/L) pH', ' N (mg/L)', ' P (mg/L)']
['61610628', ' JP20B', ' 23/06/2000', ' 3.71', ' 430', ' 8.8', ' 0.28', ' 0.007']
['61610628', ' JP20B', ' 27/10/2000', ' 3.18', ' 610', ' 7.2', ' 1.3', ' 0.019']

In height function...
Water level:  3.71
Water level:  3.18
61610628
Average Water Level: 3.445
Minimum Nitrogen: 0.28
Maximum Phosphorous 8.8

希望这能为您解决问题。

答案 1 :(得分:0)

从跟踪中看来,传递给函数的参数似乎不是列表,而是函数。您调用height(...)的代码将是错误所在的位置。

很抱歉没有将此作为评论发布,我没有这样做的声誉。