Saltar al contenido

~ / archivo / stackoverflow / 56060981

Program only reads first row of csv file

StackOverflow · python · ayudé
  1. Hao pregunta +1 2019-05-09 13:51

    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()
    
  2. RomanPerekhrest respuesta aceptada +1 2019-05-09 14:06

    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.

    • Hao 2019-05-10 04:21 thanks, tested it out and it was indeed the line causing issues, have fixed and solved my code as well, cheers
  3. Miguel Ortiz yo respuesta 2019-05-09 14:03

    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.

    • Hao 2019-05-09 16:27 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..
    • Miguel Ortiz yo 2019-05-09 18:56 You should add an example of the desired output because I don't understand what you want to achieve.

← todos los hilos de StackOverflow