سؤال

This is what I have so far:

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

I am creating a csv that looks like this:

  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.

I am trying to sort the CSV alphanumerically by the first column, but I can't get the sortedlist statement to work.

Thanks.

هل كانت مفيدة؟

المحلول

Your first problem is:

for unit, statement in reader:
    print unit

reader is the original reader you created for the Links.csv file here. I have to guess, because of the lack of indentation in your question, but I'm assuming this later code is outside the with statement. If so, you're trying to iterate over an already-closed file, which will raise an exception, which I assume is what you're seeing (although that's just another guess, since you haven't told us).

If I've guessed wrong, and this is still inside the with statement, then you have a different problem: Link Statements.csv hasn't been closed yet, so it hasn't been flushed, so when you open a new handle to it, it may have nothing in it, or only the first 7-1/2 rows, or anything else. If that's the case, just dedent this code out of the with.

At any rate, you probably meant data, not reader.

--

But if you fix that, it'll just cause a new problem. A csv.reader is an iterator—you can only iterate over it once. So, this will print out every row, and then sorted will sort whatever's left over after you've already taken everything, meaning you'll get an empty list.

If you really need to print out all the values, and then sort all the values, you'll need to put them into a list, like this:

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

While we're at it, it's worth noting that you're leaking the Link Statements.csv file here; it's much better to use a with statement, as you do earlier in your code.


Finally, you don't do anything with the sortedlist at the end except reference it. Presumably you wanted to print it, generate a new CSV file with it, or do something else, rather than nothing at all.


You didn't give us your source data, but I can copy and paste your intermediate data into something called Link Statements.csv and then run the second half of your code. If I do that, then run this:

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

… I get this:

['  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.']

So, your sorted line seems to be exactly what you wanted; it's everything around it that you got wrong.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top