1

Ok basically I have 2 lines of data

name    tribe   id  Air Water   Earth   Fire
Pema    Xero    X14C    24  54  34  43
Otaku   Taru    T111F   54  78  65  78

Currently, my code checks the column 'id' for errors according to certain conditions, but my code only reads the first row(Pema's row) and stops. I have no idea what is causing this, any help to fix this issue will be appreciated.

import csv
filePath ="data3.csv"


length="Length of Avatar ID is not 5 "
name_tribe= "Invalid first letter"
score_grade="Invalid last letter"
mid_integers="Invalid integers"

def isValidAvatarIDFormat():
    with open(filePath) as csvfile:
        reader = csv.DictReader(csvfile)
        remarks=[] 
        for row in reader:

            if(len(row['id'])!=5):
                remarks.append(length)


            if(row['tribe'][0] != row['id'][0]):
                remarks.append (name_tribe)


            avg_score= 0
            user,tribe,id, *scores= row.values()
            if all(score.isdigit() for score in scores):
                average= sum([int(score) for score in scores])/4
                if (average >=80):
                    avg_score="A"
                elif (average >=70 and average <80):
                    avg_score="B"
                elif (average >=60 and average <70):
                    avg_score="C"
                elif (average >=50 and average <60):
                   avg_score="D"                    
                elif (average >=40 and average <50):
                   avg_score="E"
                else:
                    avg_score="F"
            if(avg_score != row['id'][-1]):
                    remarks.append (score_grade)


            if (row['id'][1].isdigit() and row['id'][2].isdigit() and row['id'][3].isdigit()):
                    continue
            else:
                remarks.append (mid_integers)

            if (len(remarks) == 1):
                print (remarks)
            elif (len(remarks) >1):
                for i in range(0, len(remarks)):
                    print(remarks[i])

            del remarks 
            remarks =[]           


print("{0:<10}{1:^18}{2:^15}".format("Avatar Name  |","Avatar ID |","Comments"))        
isValidAvatarIDFormat()
0

2 Answers 2

1

The following condition:

if (row['id'][1].isdigit() and row['id'][2].isdigit() and row['id'][3].isdigit()):
    continue

within your crucial for loop makes it skip a row which id column's value has digits in selected positions (as it happened with value T111F)

T111F - symbols in positions 1,2 and 3 - are digits, which makes the loop skip the iteration with continue operator.

Your case is not related to a row position but to a particular symbols in selected positions of id column.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, tested it out and it was indeed the line causing issues, have fixed and solved my code as well, cheers
0

One of your conditionals is removing one line, but all rows are getting read:

mortiz@florida:~/Documents/projects/python/testo$ python3 test.py 
Avatar Name  |   Avatar ID |       Comments    
OrderedDict([('name', 'Pema'), ('tribe', 'Xero'), ('id', 'X14C'), ('Air', '24'), ('Water', '54'), ('Earth', '34'), ('Fire', '43')])
Length of Avatar ID is not 5 
Invalid last letter
Invalid integers
OrderedDict([('name', 'Otaku'), ('tribe', 'Taru'), ('id', 'T111F'), ('Air', '54'), ('Water', '78'), ('Earth', '65'), ('Fire', '78')])

Because it's a CSV I've formatted it like this:

name,tribe,id,Air,Water,Earth,Fire
Pema,Xero,X14C,24,54,34,43
Otaku,Taru,T111F,54,78,65,78

name,tribe,id, Pema,Xero,X14[C] <-- this is expecting an integer.

2 Comments

uh, so how do i get the error messages to show up after reading the 2nd row of the csv? I really have no idea how to go about doing that..
You should add an example of the desired output because I don't understand what you want to achieve.

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.