我需要把不同长度的行转换成一本字典。这是用来统计球员数据的。文本文件的格式如下所示。我需要返回一个字典和每个球员的统计。在
{Lebron James:(25,7,1),(34,5,6), Stephen Curry: (25,7,1),(34,5,6), Draymond Green: (25,7,1),(34,5,6)}
数据:
^{pr2}$我需要帮助启动代码。到目前为止,我有一个代码,删除空白行,并将其列为一个列表。在
myfile = open("stats.txt","r")
for line in myfile.readlines():
if line.rstrip():
line = line.replace(",","")
line = line.split()
下面是一个简单的方法:
我想这应该是你想要的:
格式有点模棱两可,因此我们必须对名称可能是什么做出一些假设。我假设名称在这里不能包含逗号,但是如果需要的话,可以稍微放松一下,尝试解析int、int、int,如果解析失败,就把它当作名称处理。在
也许是这样的:
对于Python 2.x
对于Python 3.x
^{pr2}$相关问题 更多 >
编程相关推荐