Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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文件中查找每个文件的前5名_Python_Python 3.x - Fatal编程技术网

从python文件中查找每个文件的前5名

从python文件中查找每个文件的前5名,python,python-3.x,Python,Python 3.x,我有一个文本文件,上面写着每年男性和女性的前五名,我需要找到每一年的前五名,到目前为止我有一点,但还不够,我能得到一些帮助吗 # [import statements] import q1fun # [constants] # [rest of program code] f = open("customers.txt", "r", encoding="utf-8") q1fun.names(f) def names(f): """ ---------------------

我有一个文本文件,上面写着每年男性和女性的前五名,我需要找到每一年的前五名,到目前为止我有一点,但还不够,我能得到一些帮助吗

# [import statements]
import q1fun
# [constants]

# [rest of program code]
f = open("customers.txt", "r", encoding="utf-8")
q1fun.names(f)

def names(f):
    """
    -------------------------------------------------------
    [function description]
    -------------------------------------------------------
    Preconditions:
       [parameter name - parameter description (parameter type and constraints)]
    Postconditions:
       [returns: or prints:]
       [return value name - return value description (return value type)] 
    -------------------------------------------------------
    """
    f.seek(0)
    line = f.readline().strip()
    values = line.split(",")
    line_best = float(values[2])
    l = values

    if line_best == "m":
        for line in f:
            values = line.split(",")
            if line_best < float(values[3]):
                line_best = float(values[3])
                l = values

    else:
        for line in f:
            values = line.split(",")
            if line_best < float(values[3]):
                line_best = float(values[3])
                l = values

    return
#[导入语句]
进口乐趣
#[常数]
#[程序代码的其余部分]
f=打开(“customers.txt”,“r”,encoding=“utf-8”)
Q1.姓名(f)
def名称(f):
"""
-------------------------------------------------------
[功能描述]
-------------------------------------------------------
先决条件:
[参数名称-参数说明(参数类型和约束)]
后条件:
[返回:或打印:]
[返回值名称-返回值说明(返回值类型)]
-------------------------------------------------------
"""
f、 搜索(0)
line=f.readline().strip()
值=行分割(“,”)
行\最佳=浮动(值[2])
l=值
如果行_best==“m”:
对于f中的行:
值=行分割(“,”)
如果行_最佳<浮动(值[3]):
行\最佳=浮动(值[3])
l=值
其他:
对于f中的行:
值=行分割(“,”)
如果行_最佳<浮动(值[3]):
行\最佳=浮动(值[3])
l=值
返回
使用读取格式;它毕竟是用逗号分隔的。Python使用基于0的索引,因此计数是索引2处的列,而不是索引3处的列

A是跟踪给定序列的前N个值的最有效方法

import csv
import heapq

male = []
female = []

with open('customers.txt', newline='') as infile:
    reader = csv.reader(infile)
    for row in reader:
        name, sex, count = row[0], row[1], int(row[2])
        heap = male if sex.lower() == 'm' else female
        if len(heap) < 5:
            heapq.heappush(heap, (count, name))
        else:
            heapq.heappushpop(heap, (count, name))

    # `male` and `female` now hold the 5 most common names
    # each list is a sequence of (count, name) tuples
    return sorted(male), sorted(female)
导入csv
进口heapq
男性=[]
女性=[]
使用open('customers.txt',换行符='')作为填充:
reader=csv.reader(infle)
对于读取器中的行:
姓名、性别、计数=第[0]行、第[1]行、整数(第[2]行)
heap=男性,如果是性别。lower()==“m”else女性
如果len(heap)<5:
heapq.heappush(堆,(计数,名称))
其他:
heapq.heappushpop(堆,(计数,名称))
#“男”和“女”现在拥有5个最常见的名字
#每个列表都是(计数、名称)元组序列
返回已排序(男性),已排序(女性)

customers.txt的格式是什么?你能发一点吗?这看起来像是家庭作业,我们不反对帮你,但如果你发尽可能多的信息,你会得到更多。艾玛,F,18787伊莎贝拉,F,18590艾米莉,F,17415奥利维亚,F,17059艾娃,F,17021麦迪逊,F,17006大流士,M,1049杰瑞,M,1044詹姆,M,1040你的代码读一行,将
line\u best
设置为第四项的浮点值,然后尝试将该浮点值与
'm'
进行比较。这永远不会起作用。我一直收到heapq的一个错误,说它是一个未使用的变量抱歉,
导入
行丢失了,现在添加了。我一直在这里收到这个错误。错误:迭代器应该返回字符串,而不是字节(你是在文本模式下打开文件的吗?);我已将该信息作为标记添加到您的问题中,并更新了我的答案以使用Python 3。您如何将列表反过来