意外的缩进错误

时间:2014-02-21 12:41:55

标签: python python-2.7 indentation

一般来说,对于python /编程很新,一直在编写一个脚本,但是在for line in csv.reader( open(filename), delimiter="\t"):行周围遇到了缩进错误,他们正在尝试一些事情,但可以使用一些帮助来解决它,任何想法?

你能解释一下你有什么回答,帮助学习过程谢谢!

#!/usr/bin/python
import csv
import pprint
pp = pprint.PrettyPrinter(indent=4)
import sys
import getopt
import re

changes = {}

import argparse
parser = argparse.ArgumentParser()

parser.add_argument ("infile", metavar="CSV", nargs="+", type=str, help="data file") 
args = parser.parse_args()

sample_names = []

SIMILARITY_CUTOFF = 95

#
# Function that investigates the similarity between two samples. 
#
#
def similar_samples( sample_name1, sample_name2):

    combined_changes = dict()

    for change, fraction in changes[ sample_name1 ]:
        if ( change not in combined_changes):
            combined_changes[change] = []

        combined_changes[change].append(float(fraction))

    for change, fraction in changes[ sample_name2 ]:
        if ( change not in combined_changes):
            combined_changes[change] = []
        combined_changes[change].append(float(fraction))


    passed_changes = 0
    failed_changes = 0

    for change in combined_changes.keys():

        if ( len(combined_changes[ change ]) == 1):
            failed_changes +=1
            continue

        sum = 0
        count = 0
        for a in combined_changes[ change ]:

            sum += a
            count += 1

            mean = sum/ count


        for a in combined_changes[ change ]:
            if ( mean > a + 2  or mean < a - 2):
                failed_changes += 1
            else:
                passed_changes += 1


#    print "passed changes: %d, failed changes: %d" % ( passed_changes, failed_changes)


    if ( passed_changes * 100 / (passed_changes + failed_changes) > SIMILARITY_CUTOFF):
        print " vs ".join([sample_name1, sample_name2]) + " : Similar samples"
        return 1
    else:
        print " vs ".join([sample_name1, sample_name2]) + " : Different samples"
        return 0



#     print "mean %.2f \n" % ( sum/ count)






for filename in args.infile:
    sample_name = filename
    #sample_name = re.search("^(.*)\_", filename).group(1)
    changes[ sample_name ] = []
sample_names.append( sample_name )

    for line in csv.reader( open(filename), delimiter="\t"):
        for item in line[2:]:

            if not item.strip():
                continue

            item = item.split(":")
            item[1] = item[1].rstrip("%")

            changes[ sample_name].append([line[1]+item[0],item[1]])

for i in range(0, len(sample_names)):
    for j in range(i+1, len(sample_names)):

        similar = similar_samples( sample_names[ i ], sample_names[ j ])


exit()

2 个答案:

答案 0 :(得分:2)

缩进错误。

尝试缩进

sample_names.append( sample_name )

线

答案 1 :(得分:1)

您引用的那一行之前的行应该是一个更多的缩进

  

sample_names.append(sample_name)

这一行我的意思是:)