好的,基本上我有2行数据
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()
答案 0 :(得分:1)
以下情况:
5__________100
3__________99
2__________98
1__________97
4__________96
1__________95
2__________94
3__________93
1__________92
4__________91
3__________90
2__________89
5__________88
2__________87
3__________86
4__________85
1__________84
3__________83
2__________82
5__________81
3__________80
在关键的if (row['id'][1].isdigit() and row['id'][2].isdigit() and row['id'][3].isdigit()):
continue
循环内,它跳过了其中for
列的值在选定位置具有数字的行(因为值id
发生了这种情况)
T111F
-位置1,2和3的符号是数字,这使得循环使用T111F
运算符跳过迭代。
您的情况与行位置无关,但与continue
列的选定位置中的特定符号有关。
答案 1 :(得分:0)
您的条件之一是删除一行,但所有行都已被读取:
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
名称,部落,ID, Pema,Xero,X14 [C] <-这是一个整数。