Python缩进错误

时间:2013-09-08 22:14:06

标签: python

我每次都会使用此代码获得缩进错误:

    if resulttaglist[1].childNodes[0].toxml() == 0:
    sys.exit
else:
    subdomaintaglist = doc.getElementsByTagName('subdomain')
    subdomain = subdomaintaglist[0].childNodes[0].toxml()
        maindomaintaglist = doc.getElementsByTagName('DOMAIN')
        maindomain = maindomaintaglist[0].childNodes[0].toxml()
    domain =  subdomain + '.' + maindomain
    usertaglist = doc.getElementsByTagName('USER')
    user = usertaglist[0].childNodes[0].toxml()
        docroot, yip, alias = createvhosts.getvars(domain)
        serverip = createvhosts.getmainip()
                if yip == serverip:
                    createvhosts.writeconfshared(user, domain, docroot, yip, alias)
                else:
                    createvhosts.writeconfded(user, domain, docroot, yip, alias)
        proc = subprocess.Popen("/etc/init.d/nginx restart > /dev/null 2>&1", shell=True)

每次我试图纠正它都行不通,有人能告诉我什么是错的吗? 感谢

2 个答案:

答案 0 :(得分:1)

尝试:

if resulttaglist[1].childNodes[0].toxml() == 0:
    sys.exit()
else:
    subdomaintaglist = doc.getElementsByTagName('subdomain')
    subdomain = subdomaintaglist[0].childNodes[0].toxml()
    maindomaintaglist = doc.getElementsByTagName('DOMAIN')
    maindomain = maindomaintaglist[0].childNodes[0].toxml()
    domain =  subdomain + '.' + maindomain
    usertaglist = doc.getElementsByTagName('USER')
    user = usertaglist[0].childNodes[0].toxml()
    docroot, yip, alias = createvhosts.getvars(domain)
    serverip = createvhosts.getmainip()
    if yip == serverip:
        createvhosts.writeconfshared(user, domain, docroot, yip, alias)
    else:
        createvhosts.writeconfded(user, domain, docroot, yip, alias)
    proc = subprocess.Popen("/etc/init.d/nginx restart > /dev/null 2>&1", shell=True)

确保您的编辑器仅保存空格或标签,但不能同时保存两者。我个人的偏好是空格。

答案 1 :(得分:0)

我认为您正在混淆制表符和空格。如果我没有记错的话,如果/ else不正确,则会导致缩进。 试试这个:-

if resulttaglist[1].childNodes[0].toxml() == 0:
    sys.exit
else:
    subdomaintaglist = doc.getElementsByTagName('subdomain')
    subdomain = subdom.........