缩进错误:应为缩进块(在IF循环中添加“IF”后弹出)

2024-09-28 20:55:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写这个简短的脚本是为了在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


Tags: csv字符串name脚本ifstatus错误open
2条回答

您需要正确缩进代码。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=%E2%9D%AE%20Python%20Glossary-,Python%20Indentation,indicate%20a%20block%20of%20code

相关问题 更多 >