简体   繁体   English

如何在python中创建的csv文件中按字母顺序对数据进行排序?

[英]How to sort data alphabetically in a csv file created in python?

The name of a user and their score from a quiz is entered in a csv file. 用户名及其测验的分数将输入到csv文件中。 I want to sort the name of the user alphabetically, however I don't know how to do this. 我想按字母顺序对用户名进行排序,但是我不知道该怎么做。 Thanks in advance. 提前致谢。 Here are snippets of my code. 这是我的代码片段。

userName=input('Please enter your full name: ').title()
newrecord = "{user_name},{score_1},{score_2},{score_3}\n".format(user_name=userName, score_1=quiz_scores[0], score_2=quiz_scores[1], score_3=quiz_scores[2])
classa = input("What class are you in? ")
if classa =='1':
    file=open('classroom1.csv', "a+")
    file.write(newrecord)
    file.close()
    with open('classroom1.csv') as csvfile:
        readCSV = csv.reader(csvfile)

I'm assuming that given a csv file of user names and quiz scores, you want to read the csv file and then sort based on user names. 我假设给定一个包含用户名和测验分数的csv文件,您想要读取csv文件,然后根据用户名进行排序。 Your problem (I believe) is that each time you read from the csv file, you get a list of the form [user_name, score1, score2, score3] . 您的问题(我相信)是,每次您从csv文件中读取数据时,都会得到[user_name, score1, score2, score3]形式的列表。

If so, why not store each such list in a dictionary, using the user_names as keys, and then sort the dictionary keys - ie 如果是这样,为什么不将每个这样的列表存储在字典中,使用user_names作为键,然后对字典键进行排序-即

sorting_dict = {}
for curr_elem in readCSV:
   sorting_dict[curr_elem[0]] = curr_elem #Note this assumes no 2 users have the same name
sorted_names = sorted(sorting_dict.keys()) # actually sorted(sorting_dict) will also work and may be preferred, but '.keys()' makes it clearer for me

Now, you can access your records in sorted order: 现在,您可以按排序顺序访问记录:

for curr_user in sorted_names:
   curr_record = sorting_dict[curr_user]
   #Do what you want from here...

==================================================== ================================================== ==

Hmmmmm... it's odd this didn't work for you. 嗯...奇怪的是这对你没用。 I made a dummy file like you described and this is what seemed to work for me: 我像您描述的那样制作了一个虚拟文件,这似乎对我有用:

>>> f=open('csv.txt','r')
>>> readCSV = csv.reader(f)
>>> for curr_elem in readCSV:
...     sorting_dict[curr_elem[0]] = curr_elem
... 
>>> sorted_names = sorted(sorting_dict.keys())
>>> sorted_names
['art', 'bob', 'dick', 'harry', 'tom']
>>> for curr_user in sorted_names:
...     print sorting_dict[curr_user]
... 
['art', '77', '99', '98']
['bob', ' 88', '99', '78']
['dick', '77', '66', '99']
['harry', '90', '78', '98']
['tom', '33', '98', '67']
>>> 

where my csv.txt file was: 我的csv.txt文件在哪里:

bob, 88,99,78
art,77,99,98
tom,33,98,67
dick,77,66,99
harry,90,78,98

the only 'gotcha' here is you need to be sure the 'harry' line doesn't end with a \\n , otherwise the csv reader will read an empty line that will throw an error at sorting_dict[curr_elem[0]] = curr_elem , but one can easily guard against that. 这里唯一的“陷阱”是您需要确保“ harry”行不以\\n结尾,否则csv阅读器将读取一个空行,这将在sorting_dict[curr_elem[0]] = curr_elem处引发错误,但您可以轻松地避免这一点。

Consider using the list.sort() function by appending csv data into a list and then sorting it by first element using a defined function. 考虑使用list.sort()函数,方法是将csv数据附加到列表中,然后使用定义的函数按第一个元素对它进行排序。 This function might even be unnecessary as Python defaults to first element in nested list, so can leave out the key argument: 由于Python默认将嵌套列表中的第一个元素作为默认值,因此该功能甚至可能是不必要的,因此可以省略key参数:

csvdata = []
with open('classroom1.csv') as csvfile:
        readCSV = csv.reader(csvfile)        
        for line in readCSV:
            csvdata.append(line)

for i in csvdata:
    print(i)
#['bravo', '93']
#['alpha', '86']
#['charlie', '67']
#['echo', '70']
#['delta', '75']

def getKey(item):
    return item[0]    

csvdata.sort(key=getKey)

for i in csvdata:
    print(i)
#['alpha', '86']
#['bravo', '93']
#['charlie', '67']
#['delta', '75']
#['echo', '70']

Why not use Pandas? 为什么不使用熊猫?

df = pd.read_csv('classroom1.csv', sep = ',', names = ['user_name','score_1','score_2','score_3'])
df = df.sort('user_name')
df.to_csv('classroom1.csv', index = False, header = False)

That should take care of it. 那应该照顾它。

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

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