0

I want to use the For Loop and print a row one by one whatever i required. here is my code:

import csv
with open("details.csv") as csvFile:
    reader = csv.DictReader(csvFile)
    for row in reader:

        if['age'] == '21':
            print(row['Name'], row['age'], row['DOB'])
        else:
            continue

Here i want run the for loop until 6 times and also i want specific data of who ever age is '21', that person details only i want print, if it is other than '21' then skip the row. but my code is doesn't perform exactly like i want. can anyone help me..? Thank you :)

my csv is:

Name    age dob 
Arun    21  01/08/93    
Banni   20  05/11/94    
charan  23  23/03/92    
nani    21  04/05/93    
7
  • So whats your output? Commented Mar 25, 2015 at 6:57
  • @kasra, it is not printing anything.. may be syntax error i guess Commented Mar 25, 2015 at 6:58
  • is it ['age'] == '21' or row['age'] == '21' Commented Mar 25, 2015 at 6:58
  • @duck, i tried but both are not working Commented Mar 25, 2015 at 7:01
  • 1
    could you give us a sample csv content Commented Mar 25, 2015 at 7:03

4 Answers 4

3

Simple error: Try this

import csv
with open("details.csv") as csvFile:
    reader = csv.DictReader(csvFile)
    for row in reader: 
        if row['age'].strip() == '21': #error in this line
            print(row['Name'], row['age'], row['DOB'])
        else:
            continue
Sign up to request clarification or add additional context in comments.

1 Comment

oh, post few top line(with header) in your csv
0

I tried your code and your file and got an error. Then I replaced tabs in your csv file with commas and capitalized "dob" to "DOB":

Name,age,DOB
Arun,21,01/08/93
Banni,20,05/11/94
charan,23,23/03/92
nani,21,04/05/93

Then the output was correct:

>>> 
Arun 21 01/08/93
nani 21 04/05/93   
>>> 

Of course I changed Line 5 to if row['age'] == '21':

Comments

0

By default DictReader uses comma , as a seperator between two fields but your csv file uses tabs.

If you don't want to change your csv file the solution is to change the creation of your DictReader to

reader = csv.DictReader(f, delimiter='\t')

Next change the line if['age'] == '21': to if row['age'] == '21':.

Finally row['DOB'] should be row['dob'] as the field names are case sensitive.

Comments

0

Try This if you dont have specified the header in csv file

import csv
import sys

f = open("File_Name.csv", 'rt') #open the file reading 
print "Name\tAge\tDOB"
try:
    reader = csv.reader(f)
    for row in reader:
        if row[1] == '21':     #Check the condition
            print "%s\t%s\t%s" %(row[0],row[1],row[2]) #print the columns
        else:
            continue
finally:
    f.close()                 #close the file at the end

and if the file has header as first line(Name,Age,DOB) then use following


    import csv #import csv package
    with open("details.csv") as csvFile: #open the file 
        reader = csv.DictReader(csvFile)
        for row in reader: #iterate the file
            if row['age'].strip() == '21': #check the condition
                print(row['Name'], row['age'], row['DOB'])
            else: #skip if not satisfies the condition
                continue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.