2014-02-21 317 views
0

對於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() 
+1

Python中不需要圍繞'if'條件的括號。 – filmor

+1

使用pylint來檢測語法錯誤,就像這樣。 E:92,0:意外縮進(語法錯誤) –

+2

如果你看到一個非常奇怪的編譯器錯誤,它看起來不能從它表示它的行中看到前一行。適用於此,也適用於其他語言中缺少分號,括號等。 – RemcoGerlich

回答

1

你報的應該是一個縮進更

sample_names.append(sample_name)

此行的一個前行我的意思是:)

+0

謝謝幫助很多! – user3234810

2

縮進錯誤。

嘗試縮進

sample_names.append(sample_name) 

+0

也謝謝你! – user3234810