Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除文件中低于特定阈值的带区_Python_Function_File_Input - Fatal编程技术网

Python 删除文件中低于特定阈值的带区

Python 删除文件中低于特定阈值的带区,python,function,file,input,Python,Function,File,Input,代码将把乐队的名字和分数写入一个文件 然后,如果乐队的分数低于用户给定的某个阈值,它应该删除文件中乐队的详细信息。希望你能帮忙 提前感谢为什么要先编写文件,然后再尝试过滤?文件I/O操作在您的逻辑中是比较昂贵的操作,所以您应该尽量减少它们。您应该在其他输入之前请求阈值,然后在文件写入循环中添加一个条件: numberofbands = int(input("How many bands are there in the competition? ")) print("Input each

代码将把乐队的名字和分数写入一个文件

然后,如果乐队的分数低于用户给定的某个阈值,它应该删除文件中乐队的详细信息。希望你能帮忙


提前感谢

为什么要先编写文件,然后再尝试过滤?文件I/O操作在您的逻辑中是比较昂贵的操作,所以您应该尽量减少它们。您应该在其他输入之前请求阈值,然后在文件写入循环中添加一个条件:

numberofbands = int(input("How many bands are there in the competition? "))



print("Input each band’s name pressing enter after each one") 

file = open("scores.txt","w") 
for loop in range(numberofbands): 
  name = input("\nEnter the name of the band: ") 
  votes = input("Enter how many votes that band received: ")
  file.write(name + "," + votes + "," + "\n") 
file.close() 

number_of_lines = len(open("scores.txt").readlines(  ))

def removebelowthreshold():
   threshold = int(input("Choose the threshold of scores to remove bands which are below"))


removebelowthreshold()

显然,要使程序健壮,您需要进行多种数据类型和范围检查,但基本逻辑是这样的。

所以您要求我们为您编写完整的逻辑?如果您尝试一下会好得多,只有当您无法让它工作时,您才应该发布一个供我们帮助的帖子。@ShadowRanger我可以通过data=line.split(“,”)和data[1]>threshold:,来查询文件。但是,我不知道如何从文件中删除这些行。感谢您的回复。我需要打印所有乐队的批发文件,然后使用单独的功能,打印乐队的名称和分数高于阈值,并删除其他乐队。所以你的建议是不可能的。在这种情况下,生成两个文件——一个是完整的列表,另一个是经过筛选的文件。仍然只有2个文件写入,而不是2个文件写入+1个文件读取,这是您想要的方法。很抱歉,这是持久性的,但问题是,我需要用户在生成文件后,在将调用的函数中输入阈值。
threshold = int(input("Choose the threshold of scores to remove bands which are below"))

numberofbands = int(input("How many bands are there in the competition? "))

print("Input each band’s name pressing enter after each one") 

file = open("scores.txt","w") 
for loop in range(numberofbands): 
    name = input("\nEnter the name of the band: ") 
    votes = input("Enter how many votes that band received: ")
    if int(votes) >= threshold:
        file.write(name + "," + votes + ",\n") 
file.close() 

number_of_lines = len(open("scores.txt").readlines(  ))