程序只读取csv fi的第一行

2024-05-15 03:45:01 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上我有两行数据

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

目前,我的代码根据某些条件检查列“id”是否有错误,但我的代码只读取第一行(Pema的行)并停止。我不知道这是什么原因,任何帮助解决这个问题将不胜感激。你知道吗

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()

Tags: andinidforlenifrowavg
2条回答

以下情况:

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

在关键的for循环中,它会跳过id列的值在选定位置有数字的行(就像值T111F一样)

T111F—位置1、2和3中的符号—是数字,这使得循环跳过使用continue运算符的迭代。你知道吗

您的大小写与行位置无关,而是与id列的选定位置中的特定符号有关。你知道吗

其中一个条件是删除一行,但所有行都被读取:

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')])

因为它是一个CSV,所以我把它格式化成这样:

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

姓名,部落,身份证, Pema,Xero,X14[C]&这需要一个整数。你知道吗

相关问题 更多 >

    热门问题