SyntaxError无效语法

时间:2015-12-08 02:28:23

标签: python

此代码有什么错误:

data = ['K:C:192.168.1.3:myusername:mypassword', 'M:N:192.168.3.2:root:toor']
for i in data:
        r = str(i).strip(":")
        if len(r) == 4:
            print r[0] + "   ||" + ((28 - len(r[1])) / 2) * " " + r[1] + ((28 - len(r[1]) / 2) * " " + "||" + ((22 - len(r[2])) / 2) * " " + r[2] + ((22 - len(r[2])) / 2) * ' ' + "||" + ((22 - len(r[3])) / 2) * " " + r[3] + ((22 - len(r[3])) / 2) * " "
        elif len(r) == 3:
            print r[0] + "   ||" + ((28 - len(r[1])) / 2) * " " + r[1] + ((28 - len(r[1]) / 2) * " " + "||" + ((22 - len(r[2])) / 2) * " " + r[2] + ((22 - len(r[2])) / 2) * ' ' + "||"

得到了这样的错误:

    elif len(r) == 3:
       ^
SyntaxError: invalid syntax

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

这只是上一行(以及下一行)中括号不匹配的简单情况。试试这个:

data = ['K:C:192.168.1.3:myusername:mypassword', 'M:N:192.168.3.2:root:toor']
for i in data:
    r = str(i).strip(":")
    if len(r) == 4:
        print r[0] + "   ||" + ((28 - len(r[1])) / 2) * " " + r[1] + ((28 - len(r[1])) / 2) * " " + "||" + ((22 - len(r[2])) / 2) * " " + r[2] + ((22 - len(r[2])) / 2) * ' ' + "||" + ((22 - len(r[3])) / 2) * " " + r[3] + ((22 - len(r[3])) / 2) * " "
    elif len(r) == 3:
        print r[0] + "   ||" + ((28 - len(r[1])) / 2) * " " + r[1] + ((28 - len(r[1])) / 2) * " " + "||" + ((22 - len(r[2])) / 2) * " " + r[2] + ((22 - len(r[2])) / 2) * ' ' + "||"