我正在编写一个代码,用于根据一组描述创建一个完整的玩家列表。有一个CSV文件,其中包含过去一年中每个NFL比赛的描述,例如" 44-J.STARKS右边防守GB 41(2-Z.CHANCELLOR,50-K.WRIGHT)。&# 34;或" 3-R.WILSON通过短暂的权利到海上的11-P.HARVIN RAN OB 29对于9码。"我还有一个跑卫的更新列表(以及将来的其他位置),如果玩家出现在跑步游戏中,我会手动添加,但不在跑回列表中。我的代码看起来像 -
rbs=open('rbs.csv', 'rb+a') #open the running back file
with open('/Users/masongardner/Desktop/pbp-2014.csv', 'rb') as csvfile: #opens the list of descriptions
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
reader.next() #skip the title row
for row in reader:
desc=row[14] #corresponds with the description of each play
#print desc
for line in rbs: #each line should be another running back in the running backs CSV
if re.findall(line[0], desc, re.I):
print 'found'
print line
print desc
elif re.findall('no play| KICKS | KNEEL | KNEELS | PUNT | PUNTS | Extra point |Field goal|Two-minute|END GAME|timeout|end of quarter|end quarter|no huddle', desc, re.I):
pass
elif re.findall('right guard|right end|right tackle|left guard|left tackle|left end|up the middle', desc,re.I):
print "did not find it"
print desc
rback=raw_input('running back:')
with open('rbs.csv','a') as rbfile:
rbwriter=csv.writer(rbfile, delimiter=' ', quotechar='|',quoting=csv.QUOTE_MINIMAL)
rbwriter.writerow([rback])
else:
print "did not find it"
print desc
rbs.close()
当我刚刚开始时,我为“rbs”提供了一个输入。档案为Eddy Lacy,应该找到谁。问题是,当我运行我的代码时,我根本没有收到任何输出。我只返回0的退出状态。
我不明白为什么会这样,因为有数千行,这应该给我一些输出。有人可以帮助我解决我的流程有什么问题吗?
此外: 就这一点而言,当我用textedit打开它时,我的rbs.csv文件的格式是这样的
27 E.LACY
24 F.GORE
等
当我查看播放说明的其中一行时,文件的格式如下: 2014090400,2014-09-04,1,13,32,GB,SEA,1,10,39,0,0,"(13:32)(NO HUDDLE)44-J.STARKS RIGHT GUARD TO GB 41 FOR FOR YARDS(31-K.CHANCELLOR,50-K.WRIGHT)。",0 ,,, 2014,2," NO HUDDLE",RUSH,1,0,0 ,0,,0,0,0,,0,0,0,0,0,0,"正确的守护",39,OWN,0,,0,0
答案 0 :(得分:1)
部分for line in rbs
仅运行文件一次的行,并且在开头只有一行(尽管第二行{{1}时可能会添加行找到了。
如果要遍历所有行,则需要在for循环中移动该文件的开头。而且没有必要打开它elif
。
+a
或者,您可以像这样打开文件,并在with open('/Users/masongardner/Desktop/pbp-2014.csv', 'rb') as csvfile: #opens the list of descriptions
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
reader.next() #skip the title row
for row in reader:
desc=row[14] #corresponds with the description of each play
#print desc
with open('rbs.csv', 'rb') as rbs: #open the running back file
for line in rbs: #each line should be another running back in the running backs CSV
if re.findall(line[0], desc, re.I):
print 'found'
print line
print desc
elif re.findall('no play| KICKS | KNEEL | KNEELS | PUNT | PUNTS | Extra point |Field goal|Two-minute|END GAME|timeout|end of quarter|end quarter|no huddle', desc, re.I):
pass
elif re.findall('right guard|right end|right tackle|left guard|left tackle|left end|up the middle', desc,re.I):
print "did not find it"
print desc
rback=raw_input('running back:')
with open('rbs.csv','a') as rbfile:
rbwriter=csv.writer(rbfile, delimiter=' ', quotechar='|',quoting=csv.QUOTE_MINIMAL)
rbwriter.writerow([rback])
else:
print "did not find it"
print desc
之前找到文件的开头。
这都不是很有效,所以除非你期望for line in rbs:
的内容不适合内存,否则我会在内存中的列表中构建该信息,然后将其循环以进行检查。并且最后只写出清单。
一旦工作正常,您应该查看的其他优化是使用rbs.csv
编译您使用过的模式并重新使用它们。这应该加快re.compile()
。
此外,您的引号字符似乎是findall
而不是"
,我不确定您为什么设置它,也不知道为什么它会起作用。