我不断地发现错误:
Traceback (most recent call last):
File "ba.py", line 13, in <module>
matchObj = re.match(r"^(\w+ \w+) batted (\d+) times with (\d+) hits and (\d+) runs", line)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 137, in match
return _compile(pattern, flags).match(string)
TypeError: expected string or buffer
line
应该使用line.strip
读取文件中的每一行
re.match
使用正则表达式查找字符串中3个组(players
、hits
、atBats
)的匹配项
matchObj.group()
应该阅读每个组,并将统计信息放在字典中的位置
如何让re.match将类型属性赋给matchObj,以便使用group()拉取并添加到playerStats()?
import re, sys, os
if len(sys.argv) < 2:
sys.exit("Usage: %s filename" % sys.argv[0])
filename = sys.argv[1]
if not os.path.exists(filename):
sys.exit("Error: File '%s' not found" % sys.argv[1])
playerStats = {'players': (0, 0, 0)}
matchObj = re.match(r"^(\w+ \w+) batted (\d+) times with (\d+) hits and (\d+) runs", line)
with open(filename) as f:
for line in f:
line = line.strip()
if player in playerStats:
playerStats[players][0] += atBat
playerStats[players][1] += hit
if player not in players:
player = matchObj.group(1)
playerStats[players][0] = atBat
playerStats[players][1] = hit
avgs = 0
else:
playerStats[players] = player
playerStats[players][0] = atBat
playerStats[players][1] = hit
playerStats[players][2] = 0
try:
player = matchObj.group(1)
atBat = matchObj.group(2)
hit = matchObj.group(3)
except AttributeError as ae:
print str(ae), "\skipping line:", line
except IndexError as ie:
print str(ie), "\skipping line:", line
#calculates averages
for players in playerStats:
avgs[player] = round(float(hits[player])/float(atBats[player]), 3)
print "%s: %.3f" % (player, avgs[player])
你正在把整个文件读成一行。因为行是一个列表,而不是字符串或缓冲区,所以会出现该错误。如果你在每一行中循环,把你的条放在for循环中。下面的示例帮助您开始。
另外,除非您显示文本文件中的示例数据,否则我无法确定您的regex是否准确。
更新: 这是一个基于你的评论和更新的工作版本。您可以根据需要随意修改:
in.txt的内容:
输出:
相关问题 更多 >
编程相关推荐