【问题标题】:Sorting csv in python not happening在python中对csv进行排序没有发生
【发布时间】:2015-06-18 00:35:49
【问题描述】:

我正在尝试在 python 中对 csv 文件进行排序。

这是我的 CSV 文件

cat SampleData.csv

OrderDate,Region,Rep,Item,Units,Unit Cost,Total
1/6/14,East,Jones,Pencil,95, 1.99 , 189.05
1/23/14,Central,Kivell,Binder,50, 19.99 , 999.50
2/9/14,"Central","Jardine","Pencil",36, 4.99 , 179.64
2/26/14,Central,Gill,Pen,27, 19.99 , 539.73
3/15/14,West,Sorvino,Pencil,56, 2.99 , 167.44
4/1/14,East,Jones,Binder,60, 4.99 , 299.40
4/18/14,Central,Andrews,Pencil,75, 1.99 , 149.25
5/5/14,Central,Jardine,Pencil,90, 4.99 , 449.10
5/22/14,West,Thompson,Pencil,32, 1.99 , 63.68
6/8/14,East,Jones,Binder,60, 8.99 , 539.40
12/4/15,Central,Jardine,Binder,94, 19.99 ," 1,879.06 "
12/21/15,Central,Andrews,Binder,28, 4.99 , 139.72

这是我的代码

import csv
import operator

f = open('SampleData.csv')

csv1 = csv.reader(f, delimiter=',')

sort = sorted(csv1, key=operator.itemgetter(6))

for eachline2 in sort:
        print eachline2

f.close()

这是我的结果:

['12/4/15', 'Central', 'Jardine', 'Binder', '94', ' 19.99 ', ' 1,879.06 ']
['12/21/15', 'Central', 'Andrews', 'Binder', '28', ' 4.99 ', ' 139.72 ']
['4/18/14', 'Central', 'Andrews', 'Pencil', '75', ' 1.99 ', ' 149.25 ']
['3/15/14', 'West', 'Sorvino', 'Pencil', '56', ' 2.99 ', ' 167.44 ']
['2/9/14', 'Central', 'Jardine', 'Pencil', '36', ' 4.99 ', ' 179.64 ']
['1/6/14', 'East', 'Jones', 'Pencil', '95', ' 1.99 ', ' 189.05 ']
['4/1/14', 'East', 'Jones', 'Binder', '60', ' 4.99 ', ' 299.40 ']
['5/5/14', 'Central', 'Jardine', 'Pencil', '90', ' 4.99 ', ' 449.10 ']
['6/8/14', 'East', 'Jones', 'Binder', '60', ' 8.99 ', ' 539.40 ']
['2/26/14', 'Central', 'Gill', 'Pen', '27', ' 19.99 ', ' 539.73 ']
['5/22/14', 'West', 'Thompson', 'Pencil', '32', ' 1.99 ', ' 63.68 ']
['1/23/14', 'Central', 'Kivell', 'Binder', '50', ' 19.99 ', ' 999.50 ']
['OrderDate', 'Region', 'Rep', 'Item', 'Units', 'Unit Cost', 'Total']

我不确定我在这里做错了什么。

我有两个问题,

  1. 没有像您看到的那样进行排序。
  2. 我在最后一行得到了标题。我想要它在第一行。

非常感谢任何帮助。

【问题讨论】:

  • 数据排序很好。看看sorted(['Total', ' 1,879.06 ', ' 999.50']) 的输出。如果您要对 csv 文件进行大量工作,您可能需要查看 pandas.pydata.org

标签: python sorting csv


【解决方案1】:

实际上,排序是完全正确的,因为您要比较的是字符串(逐个字符),而不是数字。字符串“1,879.06”位于“139.72”之前,因为它在字典上更小。

如果您想根据最后一列的数值对行进行排序,请将最后一列转换为浮点数或更改您作为key 传递的函数。请尝试以下操作:

sort = sorted(csv1, key=lambda t: float(t[6]))

但是,这将引发ValueError,因为列标题无法转换为数字。

要使标题出现在开头,您可以尝试将阅读器对象csv1 转换为列表并仅对列表的一部分进行排序,或者更改key 函数以确保不显示的字符串'不以数字开头的数字被转换为 0,这应该将它们放在其他行之前(如果所有其他行的值都大于 0)。

潜在的解决方案可能是这样的:

import csv
import operator

def key_fn(row):
    # Your strings contain both commas and dots, but commas can be removed
    return float(row[6].replace(',', ''))

f = open('SampleData.csv')

csv1 = csv.reader(f, delimiter=',')
header = next(csv1) # because csv reader supports the iterator protocol
rows = sorted(csv1, key=key_fn)

print header

for row in rows:
    print row

f.close()

要更好地处理数字中的逗号,请查看this question

【讨论】:

  • 非常感谢 hgazibara,这对我帮助很大。
猜你喜欢
  • 2011-01-06
  • 2015-06-14
  • 2017-06-23
  • 1970-01-01
  • 1970-01-01
  • 2010-12-18
  • 2012-04-12
  • 2021-06-02
  • 2016-07-01
相关资源
最近更新 更多