繁体   English   中英

IndentationError: 期望一个缩进块(在我在 IF 循环中添加“IF”后弹出)

[英]IndentationError: expected an indented block (this pops up after I add in the “IF” in the IF loop)

我编写了这个简短的脚本来自动打印出 csv 中包含“1|1”的字符串。 但是,当我添加 if status=='1|1' 时,发生了缩进错误。 我对此很陌生,有人可以帮忙吗?


inputfile = csv.reader(open('varStatus.csv','r'))
outputfile = open('errorlist.txt','w')

i=0

for row in inputfile:
    if (i > 5):
    name = row[1]
    status = row[0]
    if (status == '1|1'):
    print >>outputfile, name, status
    i+=1

我在 UNIX 上使用 python

您需要正确缩进您的代码。 Python 解释空格,因此您必须缩进每个 if 语句

for row in inputfile:
    if (i > 5):
        name = row[1]
        status = row[0]
    if (status == '1|1'):
        print >>outputfile, name, status
    i+=1

嗨,这是一个缩进错误,因为在 IF 语句之后您没有给出缩进块,因此 IF 条件没有任何执行。

inputfile = csv.reader(open('varStatus__case2_2Np_2N_hd1_Fx8Np_3L.csv','r'))
outputfile = open('errorlist.txt','w')

i=0

for row in inputfile:
    if (i > 5):
        name = row[1]
        status = row[0]
    if (status == '1|1'):
        print >>outputfile, name, status
    i+=1

我这应该工作。 此外,如果您是 Python 的新手并且还没有掌握缩进的窍门,请查看https://www.w3schools.com/python/gloss_python_indentation.asp#:~:text=%202%9D%AE%20Python% ,Python%20缩进,表示%20a%20block%20of%20code

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM