有谁知道如何解决此错误的人

时间:2019-01-22 12:34:16

标签: python

我正在尝试为我的项目运行此代码,但在try语句中给出了错误,即“ unindent与任何外部缩进级别都不匹配”

from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.tools import argparser

DEVELOPER_KEY = "Replaced_my_API_key"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

def youtube_search(options):
  youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    developerKey=DEVELOPER_KEY)


  search_response = youtube.search().list(
    q=options.q,
    part="id,snippet",
    maxResults=options.max_results
  ).execute()

  videos = []
  channels = []
  playlists = []

  for search_result in search_response.get("items", []):
    if search_result["id"]["kind"] == "youtube#video":
      videos.append("%s (%s)" % (search_result["snippet"]["title"],
                                 search_result["id"]["videoId"]))
    elif search_result["id"]["kind"] == "youtube#channel":
      channels.append("%s (%s)" % (search_result["snippet"]["title"],
                                   search_result["id"]["channelId"]))
    elif search_result["id"]["kind"] == "youtube#playlist":
      playlists.append("%s (%s)" % (search_result["snippet"]["title"],
                                    search_result["id"]["playlistId"]))

  print ("Videos:\n", "\n".join(videos), "\n")
  print ("Channels:\n", "\n".join(channels), "\n")
  print ("Playlists:\n", "\n".join(playlists), "\n")


if __name__ == "__main__":
    to_search = "Witcher 3"
    argparser.add_argument("--q", help="Search term", default=to_search)
    argparser.add_argument("--max-results", help="Max results", 
default=25)
    args = argparser.parse_args()

  **From the following statement the code is giving an error**

即“意外错误”            尝试:              youtube_search(参数)            除了HttpError,e:              打印“发生HTTP错误%d:\ n%s”%(例如,resp.status,e.content)

1 个答案:

答案 0 :(得分:0)

如果将IDendation与空格 tabs 混合使用,则会出现此错误。选择一个并坚持下去。您可以尝试两件事:

  • 为编辑器中的空白着色。在vim中,您可以使用:set list执行此操作。找到有问题的行并进行更正。
  • 在shebang中为Python提供选项 -tt #!/usr/bin/python -tt。当您在同一文件中混合标识时,这会给您额外的警告。