Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 缩进错误:未缩进与else块中的任何外部缩进级别不匹配_Python_Block_Indentation_If Statement - Fatal编程技术网

Python 缩进错误:未缩进与else块中的任何外部缩进级别不匹配

Python 缩进错误:未缩进与else块中的任何外部缩进级别不匹配,python,block,indentation,if-statement,Python,Block,Indentation,If Statement,else语句中的缩进已关闭。试试这个: ./babynames.py baby2008.html File "./babynames.py", line 51 else: ^ IndentationError: unindent does not match any outer indentation level def extract_名称(文件名): """ 给定baby.html的文件名,返回以年字符串开头的列表 后跟名称,按字母顺序排列字符串。 ['20

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

  ./babynames.py baby2008.html
  File "./babynames.py", line 51
    else:
        ^
IndentationError: unindent does not match any outer indentation level
def extract_名称(文件名):
"""
给定baby.html的文件名,返回以年字符串开头的列表
后跟名称,按字母顺序排列字符串。
['2006','Aaliyah 91','Aaron 57','Abagail 895','…]
"""
#在这里输入您的代码+++
f=打开(文件名为'rU')
对于f.readlines()中的行:
match=re.search(r'(\d\d\d\d)(只需在
else:
之前删除一个额外的空格字符,并确保两者(
if
和相应的
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

看起来您在其他位置前面还有一个空间。它必须与if处于相同的缩进级别。哦,谢谢!我在过去1小时内一直在打破我的头:)
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
if match:
   #...
 else:    # This has only ONE EXTRA 'SPACE' at start!
   #...