-1

这是我到目前为止所拥有的:

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 语句正常工作。

谢谢。

4

1 回答 1

1

你的第一个问题是:

for unit, statement in reader:
    print unit

readerLinks.csv是您在此处为文件创建的原始阅读器。我不得不猜测,因为您的问题中没有缩进,但我假设后面的代码在with语句之外。如果是这样,您正在尝试遍历一个已经关闭的文件,这将引发异常,我认为这就是您所看到的(尽管这只是另一个猜测,因为您还没有告诉我们)。

如果我猜错了,这仍然在with语句中,那么你有一个不同的问题:Link Statements.csv还没有关闭,所以它还没有被刷新,所以当你打开它的新句柄时,它可能有里面什么都没有,或者只有前 7-1/2 行,或者其他任何东西。如果是这种情况,只需将此代码从with.

无论如何,您可能的意思是data,不是reader

--

但如果你解决了这个问题,它只会导致一个新问题。Acsv.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线路似乎正是您想要的;你错了是它周围的一切。

于 2013-10-02T00:13:35.113 回答