Python:按列对csv进行排序

时间:2013-10-02 00:03:04

标签: python sorting csv sortedlist

这是我到目前为止所做的:

import csv
import operator

with open('Links.csv', 'rb') as input_file, \
open('Link Statements.csv', 'w') as output_file:

reader = csv.reader(input_file, delimiter=',', quoting=csv.QUOTE_NONE)

for row in reader:
    link_name, from_unit, to_unit, rate, type = row
    output_file.write(" %s," % (from_unit))
    output_file.write("Establish %s link %s at %s Kbps to %s.\n" % (type, link_name, rate, to_unit))
    output_file.write(" %s," % (to_unit))
    output_file.write("Terminate %s link %s at %s Kbps from %s.\n" % (type, link_name, rate, from_unit))

data = csv.reader(open('Link Statements.csv'),delimiter=',')
for unit, statement in reader:
print unit
sortedlist = sorted(data, key=operator.itemgetter(0), reverse=True)
sortedlist

我正在创建一个如下所示的csv:

  RCT-6,Establish LOS UHF MCR link MPP01 at 14336 Kbps to 1/6.
  1/6,Terminate LOS UHF MCR link MPP01 at 14336 Kbps from RCT-6.
  RCT-6,Establish SIPRNET link SIPRPP01 at 8192 Kbps to 1/6.
  1/6,Terminate SIPRNET link SIPRPP01 at 8192 Kbps from RCT-6.
  RCT-6,Establish NIPRNET link NIPRPP02 at 4096 Kbps to 2/6.
  2/6,Terminate NIPRNET link NIPRPP02 at 4096 Kbps from RCT-6.
  RCT-6,Establish BSPE link BSPEPP03 at 472 Kbps to 1/10.
  1/10,Terminate BSPE link BSPEPP03 at 472 Kbps from RCT-6.
  1/10,Establish DPV0 link DPV0PP04 at 472 Kbps to 2/6.
  2/6,Terminate DPV0 link DPV0PP04 at 472 Kbps from 1/10.
  1/6,Establish SIPRNET link SIPRPP04 at 8192 Kbps to 1/3.
  1/3,Terminate SIPRNET link SIPRPP04 at 8192 Kbps from 1/6.
  1/6,Establish NIPRNET link NIPRPP03 at 8192 Kbps to 1/10.
  1/10,Terminate NIPRNET link NIPRPP03 at 8192 Kbps from 1/6.
  3/6,Establish NIPRNET link SIPRPP03 at 4096 Kbps to 1/10.
  1/10,Terminate NIPRNET link SIPRPP03 at 4096 Kbps from 3/6.
  MEB,Establish NIPRNET link NIPRZP01 at 8192 Kbps to RCT-6.
  RCT-6,Terminate NIPRNET link NIPRZP01 at 8192 Kbps from MEB.
  MEB,Establish SIPRNET link SIPRZP01 at 4096 Kbps to RCT-6.
  RCT-6,Terminate SIPRNET link SIPRZP01 at 4096 Kbps from MEB.

我试图通过第一列按字母顺序对CSV进行排序,但我无法使sortedlist语句起作用。

感谢。

1 个答案:

答案 0 :(得分:1)

你的第一个问题是:

for unit, statement in reader:
    print unit

reader是您在此处为Links.csv文件创建的原始阅读器。我必须猜测,因为你的问题缺少缩进,但我假设后面的代码超出了with语句。如果是这样,你试图迭代一个已经关闭的文件,这将引发一个异常,我认为这是你所看到的(虽然这只是另一个猜测,因为你没有告诉我们)。

如果我猜错了,并且这仍然在with语句中,那么您有一个不同的问题:Link Statements.csv尚未关闭,所以它没有被刷新,所以当你打开一个新的句柄时,它可能没有任何内容,或只有前7-1 / 2行或其他任何内容。如果是这种情况,只需从with

中提取此代码即可

无论如何,您可能需要data,而不是reader

-

但如果你解决了这个问题,那只会引发一个新问题。 csv.reader迭代器 - 您只能迭代一次。因此,这将打印出每一行,然后sorted将对您已经取得所有内容后遗留的任何内容进行排序,这意味着您将获得一个空列表。

如果您确实需要打印出所有值,然后对所有值进行排序,则需要将它们放入列表中,如下所示:

data = list(csv.reader(open('Link Statements.csv'),delimiter=','))

虽然我们在这里,但值得注意的是你在这里泄露Link Statements.csv文件;使用with语句要好得多,就像在代码中一样。


最后,除了引用它之外,你最后不会对sortedlist做任何事情。大概你想要print它,用它生成一个新的CSV文件,或做其他事情,而不是一无所获。


您没有向我们提供您的源数据,但我可以将您的中间数据复制并粘贴到名为Link Statements.csv的内容中,然后运行代码的后半部分。如果我这样做,那么运行:

data = csv.reader(open('Link Statements.csv'),delimiter=',')
sortedlist = sorted(data, key=operator.itemgetter(0), reverse=True)
for item in sortedlist:
    print item

......我明白了:

['  RCT-6', 'Establish LOS UHF MCR link MPP01 at 14336 Kbps to 1/6.']
['  RCT-6', 'Establish SIPRNET link SIPRPP01 at 8192 Kbps to 1/6.']
['  RCT-6', 'Establish NIPRNET link NIPRPP02 at 4096 Kbps to 2/6.']
['  RCT-6', 'Establish BSPE link BSPEPP03 at 472 Kbps to 1/10.']
['  RCT-6', 'Terminate NIPRNET link NIPRZP01 at 8192 Kbps from MEB.']
['  RCT-6', 'Terminate SIPRNET link SIPRZP01 at 4096 Kbps from MEB.']
['  MEB', 'Establish NIPRNET link NIPRZP01 at 8192 Kbps to RCT-6.']
['  MEB', 'Establish SIPRNET link SIPRZP01 at 4096 Kbps to RCT-6.']
['  3/6', 'Establish NIPRNET link SIPRPP03 at 4096 Kbps to 1/10.']
['  2/6', 'Terminate NIPRNET link NIPRPP02 at 4096 Kbps from RCT-6.']
['  2/6', 'Terminate DPV0 link DPV0PP04 at 472 Kbps from 1/10.']
['  1/6', 'Terminate LOS UHF MCR link MPP01 at 14336 Kbps from RCT-6.']
['  1/6', 'Terminate SIPRNET link SIPRPP01 at 8192 Kbps from RCT-6.']
['  1/6', 'Establish SIPRNET link SIPRPP04 at 8192 Kbps to 1/3.']
['  1/6', 'Establish NIPRNET link NIPRPP03 at 8192 Kbps to 1/10.']
['  1/3', 'Terminate SIPRNET link SIPRPP04 at 8192 Kbps from 1/6.']
['  1/10', 'Terminate BSPE link BSPEPP03 at 472 Kbps from RCT-6.']
['  1/10', 'Establish DPV0 link DPV0PP04 at 472 Kbps to 2/6.']
['  1/10', 'Terminate NIPRNET link NIPRPP03 at 8192 Kbps from 1/6.']
['  1/10', 'Terminate NIPRNET link SIPRPP03 at 4096 Kbps from 3/6.']

所以,你的sorted行似乎正是你想要的;你错了就是所有它。