Python Error,IndentationError:意外缩进

时间:2013-11-27 16:44:13

标签: python

每次我尝试执行此脚本时,都会出现错误。 代码:

def getURL(): # Get tokens
  output = subprocess.Popen(["livestreamer", "twitch.tv/CHANNEL_NAME", "-j"], stdout=subprocess.PIPE).communicate()[0]
    return json.loads(output)['streams']['worst']['url'] # Parse json and return the URL parameter

def build(): # Builds a set of tokens, aka viewers
    global numberOfSockets
    global numberOfViewers
    while True:
        if numberOfSockets < numberOfViewers:
            numberOfSockets += 1
            print "Building viewers " + str(numberOfSockets) + "/" + str(numberOfViewers)
            urls.append(getURL())

错误:

  File "sript.py", line 19
    return json.loads(output)['streams']['worst']['url'] # Parse json and return the URL parameter
    ^
IndentationError: unexpected indent

1 个答案:

答案 0 :(得分:2)

缩进getURL的第二行,使其与return语句对齐:

def getURL(): # Get tokens
    output = subprocess.Popen(["livestreamer", "twitch.tv/CHANNEL_NAME", "-j"], stdout=subprocess.PIPE).communicate()[0]
    return json.loads(output)['streams']['worst']['url']

请记住,Python会严肃对待缩进,因为它使用它来确定什么是什么。