Python-基于两列的csv文件排序 [英] Python- sorting csv file based on two column

查看:183
本文介绍了Python-基于两列的csv文件排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件说, 名称oldprice oldprofit

I have a csv file say, name oldprice oldprofit

k2  6.319044375 0.6469451082    
k3  9.2603346875    0.4639390435    
k4  10.6868384375   1.1287761798    
K1  4.1458078125    0.5129700421    
k5  7.2702040625    0.6632701035    
K1  8.025495625 0.7561548244    
k6  7.73398625  1.017231759 
k7  9.8022878125    0.562983695 
k8  5.44912125  0.8532092538    
k2  9.5360690625    0.5481493305    
K1  6.48153375  1.4176140292    
K1  6.66228125  1.0444456163
k9  7.5859665625    1.276779643 
K1  8.6394253125    0.6690271589    

我已经将名称按升序排序,然后将oldprice(第3列)按降序排序. 输出应为

I have sorted name is ascending order and then oldprice (3rd column) in descending order. The output should be,

K1  6.48153375  1.4176140292        
K1  6.66228125  1.0444456163    
K1  8.025495625 0.7561548244    
K1  8.6394253125    0.6690271589        
K1  4.1458078125    0.5129700421    
k2  6.319044375 0.6469451082        
k2  9.5360690625    0.5481493305        
k3  9.2603346875    0.4639390435        
k4  10.6868384375   1.1287761798
k5  7.2702040625    0.6632701035    
k6  7.73398625  1.017231759

k7  9.8022878125    0.562983695

k8  5.44912125  0.8532092538    
k9  7.5859665625    1.276779643 

如何在python中生成它?

How to generate it in python?

推荐答案

将csv的每一行设置为列表.现在,只需按照oldprice排序即可. 获取列表,然后使用名称再次对其进行排序. 使用此答案进行排序.

Read each line of csv as set into a list. Now just sort first according to oldprice . Get the list and sort it again using name. Use this answer to sort.

由于python 2.2以上版本,排序是稳定的.由于使用oldprice排序,因此订单将保留在具有相同名称的行中(使用name排序).

Since above python 2.2, sort is stable. The order due to sort of oldprice will be maintained in rows having same name in sort using name.

这篇关于Python-基于两列的csv文件排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆