本文介绍了IndentationError:意外的非缩进为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IndentationError:意外的非缩进为什么?

IndentationError: unexpected unindent WHY???

#!/usr/bin/python
import sys
class Seq:
    def __init__(self, id, adnseq, colen):
        self.id     = id
        self.dna    = adnseq
        self.cdnlen = colen
        self.prot   = ""
    def __str__(self):
        return ">%s\n%s\n" % (self.id, self.prot)
    def translate(self, transtable):
        self.prot = ""
        for i in range(0,len(self.dna),self.cdnlen):
            codon = self.dna[i:i+self.cdnlen]
            aa    = transtable[codon]
            self.prot += aa
    def parseCommandOptions(cmdargs):
        tfname = cmdargs[1]
        sfname = cmdargs[2]
        return (tfname, sfname)
    def readTTable(fname):
        try:
            ttable = {}
            cdnlen = -1
            tfile = open(fname, "r")
            for line in tfile:
                linearr = line.split()
                codon   = linearr[0]
                cdnlen  = len(codon)
                aa      = linearr[1]
                ttable[codon] = aa
            tfile.close()
            return (ttable, cdnlen)
    def translateSData(sfname, cdnlen, ttable):
        try: 
            sequences = []
            seqf = open(seq_fname, "r")
            line = seqf.readline()
            while line:
                if line[0] == ">":
                    id = line[1:len(line)].strip()
                    seq = ""
                    line = seqf.readline()
                    while line and line[0] != '>':
                        seq += line.strip()
                        line = seqf.readline()  
                    sequence = Seq(id, seq, cdnlen)
                    sequence.translate(ttable)
                    sequences.append(sequence)
            seqf.close()
            return sequences    
    if __name__ == "__main__":
        (trans_table_fname, seq_fname) = parseCommandOptions(sys.argv)
        (transtable, colen) = readTTable(trans_table_fname)
        seqs = translateSData(seq_fname, colen, transtable)
        for s in seqs:
            print s

它说:

 def translateSeqData(sfname, cdnlen, ttable):
   ^
IndentationError: unexpected unindent

为什么?我已经检查了数千次,但找不到问题.我只使用了制表符,没有空格.另外,有时它要求定义类.可以吗?

WHY? I have checked a thousands times and I can't find the problem. I have only used Tabs and no spaces. Plus, sometimes it asks to define the class. Is that Ok?

推荐答案

这是因为您拥有:

def readTTable(fname):
    try:

try:块之后没有匹配的except块.每个try必须至少具有一个匹配的except.

without a matching except block after the try: block. Every try must have at least one matching except.

请参阅Python教程的错误和异常部分.

See the Errors and Exceptions section of the Python tutorial.

这篇关于IndentationError:意外的非缩进为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 03:14