简体   繁体   English

NBA Python文件读取

[英]nba python file reading

I have a homework assignment and we need to go through a file we created and take the players that have played more than 50 games and have an average shooting percentage of over 50 percent (which we have to calculate by dividing the two numbers). 我有一个家庭作业,我们需要检查一下我们创建的文件,并让玩过50场以上,平均投篮命中率超过50%(我们必须通过除以两个数字来计算)的球员收录。

I have formatted my code like this, because I am trying to index into the new lines I have created from the text. 我已经这样格式化了我的代码,因为我正在尝试索引从文本创建的新行。 This is not working for me and I was wondering if anyone could point me in a new direction please. 这对我不起作用,我想知道是否有人可以指出我的新方向。 Thank you. 谢谢。

f = open("stats-clean.txt", "r")
lines = f.read()
for line in lines:
    new_lines = lines.split("\n")
report = []
new_lines.remove(new_lines[0])
for item in new_lines:
    if int(item[4])/int(item[5]) > .50 and int(item[3]) >= 50:
        report.append(item)
print(report)
f.close()

I'm not sure about the format of your input, but one problem is that you're trying to compare integers to floats. 我不确定您输入的格式,但是一个问题是您试图将整数与浮点数进行比较。 if you caste item[4] and item[5] as ints eg 如果将item [4]和item [5]转换为整数,例如

int(item[4])/int(item[5]) > .50

then the resolution of the division above will always give you an int. 那么上述除法的分辨率将始终为您提供一个整数。 So... 所以...

int(4)/int(5) = 0

But, 但,

float(4)/float(5) = 0.8

So it is likely that you're not really making the comparison that you wanted, which could lead to that section of the if statement resolving to false. 因此,您可能没有真正进行所需的比较,这可能导致if语句的该部分解析为false。

Just to let you know, there is an easier way to do this, I just scripted this solution for fun. 只是想让您知道,有一种更简单的方法可以做到这一点,我只是将此脚本编写成有趣的脚本。

import csv

with open('stats-clean.txt', 'rb') as f:
    reader = csv.reader(f)
    player_data = list(reader)

for i in player_data[1:]:
    name , pos, team, gp, fgm, fga = i
    if gp > 50:
        try:
            print 'Player: {0} Average Shooting per game {1}'.format(name, ( float(gp)/float(fga) ) )
        except ZeroDivisionError as e:
            print 'ZERO SHOTS : Player {} has {} shots'.format( name, fga )

A key problem here is that new_lines is a list of strings, for example 这里的一个关键问题是new_lines是一个字符串列表,例如

>>> new_lines[0]
'Julyan Stone,PG,DEN,4,0.5,0.5'

item[4] of this string is the character 'a' so when you go to cast with int you get ValueError: invalid literal for int() with base 10: 'a' . 此字符串的item[4]是字符'a'因此当您使用int进行ValueError: invalid literal for int() with base 10: 'a'时,会得到ValueError: invalid literal for int() with base 10: 'a'

You need to split each element of new_lines using split(',') . 您需要使用split(',') new_lines的每个元素。 So for example 所以举个例子

>>> new_lines[1].split(',')
['Chris Wilcox', 'PF', 'BOS', '61', '1.8', '2.5']

Now you can do your casting as item[4] is '1.8' and perform your conditional test but use floats instead of int to turn the strings into numbers as pointed out by @GC123UNC 现在,您可以将item[4]'1.8'并进行条件测试,但是使用floats而不是int将字符串转换为@ GC123UNC指出的数字

Also notice that the loop 另请注意,循环

for line in lines:
    new_lines = lines.split("\n")

is superfluous: lines is a string: one split('\\n') gives you the list new_lines that you need. 是多余的: lines是一个字符串:一个split('\\n')会为您提供所需的列表new_lines You need to loop through the elements of new_lines and split on , so that you can do your conditional test. 您需要遍历new_lines的元素并在上拆分,以便可以进行条件测试。

Instead of remove use slicing: newlines[1:] . 代替remove使用切片: newlines[1:]

You might wish to consider using lines = f.readlines() rather than lines = f.read() . 您可能希望考虑使用lines = f.readlines()而不是lines = f.read() The latter returns a string that you need to split and then split again. 后者返回一个需要拆分的字符串,然后再次拆分。 The former gives you a list which you can strip() and split on , (something like line.strip().split(',') ) 前者给你一个列表,你可以strip()上拆分, (像line.strip().split(',')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM