简体   繁体   English

如何使用python对CSV文件进行排序

[英]How to sort a CSV file using python

I am trying to make a leader board for my python program I have sorted out writing the different scores to the leader board already However, I am having trouble finding a way that I can sort this data (Highest score at the top and lowest at the bottom)我正在尝试为我的 python 程序制作一个排行榜我已经整理出将不同的分数写入排行榜但是,我无法找到一种可以对这些数据进行排序的方法(最高分在顶部,最低分在底部)

Also, I am sorry but I do not have any code that is even vaguely functional, everything I have tried has just been incorrect另外,我很抱歉,但我没有任何功能模糊的代码,我尝试过的一切都是不正确的

Also I only have limited access to modules as it is for a school project which makes it even harder for me (I have CSV,Random,Time,)此外,我对模块的访问权限有限,因为它是一个学校项目,这对我来说更加困难(我有 CSV、随机、时间,)

Thank you so much I would really appreciate any help I can recieve非常感谢我真的很感激我能得到的任何帮助

You can read in the file with pandas, sort it by a column, and overwrite the old csv with the new values.您可以使用 Pandas 读入文件,按列对其进行排序,然后用新值覆盖旧的 csv。 The code would look similar to this:代码看起来类似于:

import pandas as pd

path = your_file_path

df = pd.read_csv(path)

df = df.sort_values(by=["column_name"], ascending=False)

df.to_csv(path)

This problem can be done in 3 parts using standard Python:这个问题可以使用标准 Python 分 3 部分完成:

  1. Read all of the data (assuming it has a header row).读取所有数据(假设它有一个标题行)。 A csv_reader() is used to parse your file and read in each row as a list of values.一个csv_reader()用于解析您的文件并将每一行作为值列表读取。 By calling list() it will read all rows as a list of rows.通过调用list()它将读取所有行作为行列表。
  2. Sort the data对数据进行排序
  3. Write all of the data (add back the header first), this time using a csv.writer() to automatically take your list of rows and write the correct format to the file.写入所有数据(首先添加回标题),这次使用csv.writer()自动获取行列表并将正确的格式写入文件。

This can be done using Python's csv library which you say you can use.这可以使用您说可以使用的 Python 的csv库来完成。 Secondly you need to tell the sort() function how to sort your rows.其次,您需要告诉sort()函数如何对行进行排序。 In this example it assumes the scores are in the second column.在这个例子中,它假设分数在第二列中。 The csv library will read each row as a list of values (starting from 0), so the score in this example is column 1. The key parameter gives sort() a function to call for each row that it is sorting. csv库会将每一行作为一个值列表(从 0 开始)读取,因此本示例中的分数是第 1 列。 key参数为sort()了一个函数,用于为正在排序的每一行调用。 The function receives a row and returns which parts of the row to sort on, that way you don't have to sort on the first column.该函数接收一行并返回要对行的哪些部分进行排序,这样您就不必对第一列进行排序。 lambda is just shorthand for writing a single line function, it takes a parameter x and returns the elements from the row to sort on. lambda只是编写单行函数的简写,它接受一个参数x并返回要排序的行中的元素。 Here we use a Python tuple to return two elements, the score and the name.这里我们使用 Python 元组返回两个元素,分数和名称。 First convert the score string x[1] into an integer.首先将分数字符串x[1]转换为整数。 Adding a - will make the highest score sort to the top.添加-将使最高分排在最前面。 x[0] then uses the Name column to sort for cases where two scores are the same: x[0]然后使用Name列对两个分数相同的情况进行排序:

import csv

with open('scores.csv', newline='') as f_input:
    csv_input = csv.reader(f_input)
    header = next(csv_input)
    data = list(csv_input)

data.sort(key=lambda x: (-int(x[1]), x[0]))

with open('scores_sorted.csv', 'w', newline='') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(header)
    csv_output.writerows(data)

So for a sample CSV file containing:因此,对于包含以下内容的示例 CSV 文件:

name,score
fred,5
wilma,10
barney,8
betty,4
dino,10    

You would get a sorted output CSV looking like:你会得到一个排序的输出 CSV,如下所示:

name,score
dino,10
wilma,10
barney,8
fred,5
betty,4

Note, dino and wilma both have the same score, but dino is alphabetically first.请注意, dinowilma都具有相同的分数,但dino是按字母顺序排列的。

This assumes you are using Python 3.x这假设您使用的是 Python 3.x

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

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