python写入csv文件两列,Python-基于两列的csv文件排序

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

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

How to generate it in python?

解决方案

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值