简体   繁体   中英

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

I wrote this short script to automatically print out strings in csv that contains "1|1". However, when I added in the if status=='1|1', the indentation error happened. I'm quite new to this, anyone can help?


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

I'm using python on UNIX

You need to indent your code properly. Python interprets whitespace, so you must indent each if statement

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

Hi it is an indentation error because after the IF statements you haven't given an indentation block because of which the IF condition has nothing to execute.

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

I this this should work. Additionally if you are new to Python and haven't gotten the hang of indentations check out https://www.w3schools.com/python/gloss_python_indentation.asp#:~:text=%E2%9D%AE%20Python%20Glossary-,Python%20Indentation,indicate%20a%20block%20of%20code .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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