0

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

Benny L
  • 3
  • 2
  • 1
    The line of code following an `if` statement must be indented at least one space. This code has two `if` statements, and the following lines of code are not indented. – John Gordon Jul 08 '20 at 02:52
  • Also you have mixed tabs and spaces. See [IndentationError expected an indented block](https://stackoverflow.com/q/10238770/4518341) – wjandrea Jul 08 '20 at 02:56
  • https://www.python-course.eu/python3_blocks.php all programming languages have block structure. python does it with indentation. You have started blocks with `:` then have not indented blocks. clearly you could also use single line syntax is applicable – Rob Raymond Jul 08 '20 at 02:58
  • 1
    Welcome to SO! Check out the [tour], and [ask] if you want advice. Please make sure to try some research for future questions. Googling the error message is usually a good first step. BTW note that Python 2 hit end-of-life in January, so don't bother learning it unless you need to. Python 3 is much better anyway. – wjandrea Jul 08 '20 at 02:59

2 Answers2

0

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
GLaw1300
  • 195
  • 9
0

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.