0

I started using Python and have the following problems here.

  1. Indentation Error with count +=1 and several other lines

  2. Not scanning all .csv files in a directory. When I run this script, it only shows outputs of only one .csv file instead of multiple .csv file outputs on the first column. There must be an issue with for loop commands that I have.

  3. I need to take the standard deviation of each line of the fileand take the mean value of standard deviation of all the lines in each file.

    #!/usr/bin/env python
    
    import os
    
    print "Filename, Min, Max, Average, Mean of Std"
    for file in os.listdir("."):
        if not file.endswith(".csv"):
                continue    
        csv = open(file)    
        sum = 0
        diff = 0
        std = 0
        sumstd = 0
        count = 0
        min = 0
        max = 0
    
        for line in csv.readlines():
            x = line.split(",")
            time  = x[0]
            value = float(x[1])
            sum  += value       
            if value > max:
                max = value
    
            if 0 < value < min:
                min = value 
                count += 1
            avg = sum / count   
    
        import math
                count +=1
                diff += (value - avg)**2
                std = math.sqrt (diff / (count+1)-1)
                sumstd += std
                meanstd = sumstd/count
    
    
    print file + "," + str(min) + "," + str(max) + "," + str(avg) +  "," + str(meanstd)    
    
Python_R
  • 41
  • 2
  • 9

2 Answers2

3

You have used sum as a variable name, but this will hide the built-in sum function. Hiding built-ins is naturally discouraged.

  1. Indenting is important in Python. The line import math is indented only as much as for line in csv.readlines():, so the body of the for loop ends with the previous line. The recommended place for imports is at the start of the script, like you have done with import os.

  2. You have:

    if file.endswith(".csv"):
        continue    
    

    Therefore, it will skip and file whose name ends with ".csv". Don't you mean:

    if not file.endswith(".csv"):
        continue    
    

    Note that this is case-sensitive.

    By the way, the recommended way of reading CSV files is with the csv module.

MRAB
  • 20,356
  • 6
  • 40
  • 33
  • I made that change. Thank you. Do you know happen to know why I keep getting errors with count +=1 for mean of standard deviation calculations? – Python_R Aug 08 '12 at 23:09
0

Going off how SO formatted your question it looks like you may have an extra space starting at for line in csv.readlines():. An extra space will account for your indentation error. As for everything else, you will need to fix your formatting for us to be of any help. Python relies on whitespace, so make sure you keep it intact.

D.A
  • 2,555
  • 1
  • 14
  • 10