IndentationError:unindent与else块中的任何外部缩进级别都不匹配

时间:2012-10-06 17:52:32

标签: python block indentation if-statement

def extract_names(filename):
  """
  Given a file name for baby.html, returns a list starting with the year string
  followed by the name-rank strings in alphabetical order.
  ['2006', 'Aaliyah 91', Aaron 57', 'Abagail 895', ' ...]
  """
  # +++your code here+++
  f = open(filename,'rU')
  for line in f.readlines():
      match = re.search(r'(\d\d\d\d)(</h2)',line)
      if match:
          year = match.group(1)
          print line
          print year
       else:
            match = re.search(r'<td>(\d+)</td><td)(\w+)</td><td>(\w+)</td)',line)
            if match:
                rank = match.group(1)
                boyn = match.group(2)
                girln = match.group(3)
                print rank, boyn, girln
                print year


  f.close()
  return

收到以下错误,

  ./babynames.py baby2008.html
  File "./babynames.py", line 51
    else:
        ^
IndentationError: unindent does not match any outer indentation level

2 个答案:

答案 0 :(得分:1)

只需在else: 之前删除 一个额外的空格字符,并确保(if和相应的else)<强>完全相同的缩进

if match:
   #...
 else:    # This has only ONE EXTRA 'SPACE' at start!
   #...

答案 1 :(得分:0)

您的else语句中的缩进已关闭。试试这个:

def extract_names(filename):
  """
  Given a file name for baby.html, returns a list starting with the year string
  followed by the name-rank strings in alphabetical order.
  ['2006', 'Aaliyah 91', Aaron 57', 'Abagail 895', ' ...]
  """
  # +++your code here+++
  f = open(filename,'rU')
  for line in f.readlines():
      match = re.search(r'(\d\d\d\d)(</h2)',line)
      if match:
          year = match.group(1)
          print line
          print year
      else:
          match = re.search(r'<td>(\d+)</td><td)(\w+)</td><td>(\w+)</td)',line)
          if match:
              rank = match.group(1)
              boyn = match.group(2)
              girln = match.group(3)
              print rank, boyn, girln
              print year


  f.close()
  return