迭代文件中的行以检查正确的值(Python)
该文件采用以下格式:迭代文件中的行以检查正确的值(Python),python,Python,该文件采用以下格式: Britany 6.06 5.31 4.34 8.60 4.14 3.12 3.53 5.16 Eula 6.46 9.84 7.17 4.89 6.24 8.82 4.31 9.08 Georgianna 0.52 6.95 6.67 5.54 8.27 0.57 8.42 2.76 Emilee 2.66 5.73 3.29 1.27 2.66 9045 1.16 2.81 Serina 3.07 9.22 3.59 0.89
Britany 6.06 5.31 4.34 8.60 4.14 3.12 3.53 5.16
Eula 6.46 9.84 7.17 4.89 6.24 8.82 4.31 9.08
Georgianna 0.52 6.95 6.67 5.54 8.27 0.57 8.42 2.76
Emilee 2.66 5.73 3.29 1.27 2.66 9045 1.16 2.81
Serina 3.07 9.22 3.59 0.89 3.91 9.79 6.48 7.81
我需要做的是创建一个函数来检查每个参赛者的分数是否在0到10之间。如果参赛者的所有分数都可以接受,则该参赛者及其分数将写入干净的数据文件,如果不能接受,则该参赛者将被淘汰,其数据不会写入干净的数据文件。被淘汰选手的姓名和分数应存储在列表中
以下是我目前的代码:
def cleanData(userIn,userOut):
fileIn = open(userIn,'r',encoding = 'UTF8')
fileOut = open(userOut,'w',encoding = 'UTF8')
eliminated=[]
for line in fileIn:
tempList= line.rsplit(maxsplit=-9)
for num in tempList:
if num in range(0,11):
userOut.write(line)
else:
eliminated.append(line)
我试图做的是读取文件中的行并将其拆分为一个列表,这样我就可以迭代这些数字。然后,我尝试检查每个数字是否符合有效分数的标准,如果所有数字都符合此目标,则将该行写入输出文件。否则,我想将该行附加到空列表中,以便以后使用。我不确定是否正确使用了maxsplit,但我相信我是从最右边的索引-1和wan开始,到-8结束,这将创建一个只包含数字的列表。拆分行并使用
spl[1:://code>将允许您访问每个分数,all
将检查每个分数是否在阈值内
with open(in) as f,open(out,"w") as f1:
eliminated = []
for line in f:
spl = line.split() # split line into name and individual scores
if all(0 <= x <= 10 for x in [float(x) for x in spl[1:]]): # compare spl[1:] which are all the floats/scores
f1.write(line)
else:
eliminated.append(line)
打开(in)为f,打开(out,“w”)为f1:
消除=[]
对于f中的行:
spl=line.split()#将行拆分为名称和单个分数
如果全部(0
将为您提供名称后的所有值
if all([0 <= float(x) <= 10 for x in tempList]):
因为如果第一个值是好的,那么这将写入该行(在随后的每个值上也是好的!)该行:
如果num在范围内(0,11):
仅检查数字是否为该范围内的整数。
在本例中,我将使用for/else:
for line in fileIn:
scores = line.split()[1:]
for score in scores:
if not 0 < float(score) < 10:
eliminated.append(line)
break
else:
userOut.write(line)
对于fileIn中的行:
分数=line.split()[1:]
对于分数中的分数:
如果不是0<浮动(分数)<10:
删除。追加(行)
打破
其他:
userOut.write(行)
else子句只有在for子句耗尽后才会被命中,而不会命中break语句。问题在于脚本中的以下代码行:
if num in range(0,11):
它将创建一个列表[0,1,2,3,4,5,6,7,8,9,10],并且您给定的数字不在此列表中,因此此if条件将永远不会产生真值,并且不会向userOut文件写入任何内容。因此,将此行替换为:
if all([0 <= float(x) <= 10 for x in tempList]):
if all([0我修复了您的代码。以下是我更改的内容:
- 使用而不是
打开
(使用此库很容易使用utf8)
- 使用了
line.split()[1://code>而不是line.rsplit(…)
- 将每个unicode字符串强制转换为浮点数
- 如果num<0或num>10:
固定打字错误:fileOut.write
而不是userOut.write
将else
块从if…else
移动到for…else
结果如下:
import codecs
def cleanData(userIn,userOut):
fileIn = codecs.open(userIn, 'r', encoding='UTF8')
fileOut = codecs.open(userOut, 'w', encoding='UTF8')
eliminated = []
for line in fileIn:
tempList = line.split()[1:]
for item in tempList:
num = float(item)
if num < 0 or num > 10:
eliminated.append(line)
break
else: # no break
fileOut.write(line)
导入编解码器
def cleanData(用户输入、用户输出):
fileIn=codecs.open(userIn'r',encoding='UTF8')
fileOut=codecs.open(userOut'w',encoding='UTF8')
消除=[]
对于文件中的行:
tempList=line.split()[1:]
对于tempList中的项目:
num=浮动(项目)
如果num<0或num>10:
删除。追加(行)
打破
否则:#不许休息
fileOut.write(行)
尝试类似于line.split()[1:][/code>——只需拆分行并删除第一个条目(名称)。
if num in range(0,11):
if all([0 <= float(x) <= 10 for x in tempList]):
import codecs
def cleanData(userIn,userOut):
fileIn = codecs.open(userIn, 'r', encoding='UTF8')
fileOut = codecs.open(userOut, 'w', encoding='UTF8')
eliminated = []
for line in fileIn:
tempList = line.split()[1:]
for item in tempList:
num = float(item)
if num < 0 or num > 10:
eliminated.append(line)
break
else: # no break
fileOut.write(line)