-2
排序的東西,從csv取值和在一個句子並將其寫入到一個新的CSV工作:閱讀CSV,新的CSV寫信息,並通過柱
import csv
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))
這需要以下內容:
MPP01,RCT-6,1/6,14336,LOS UHF MCR
SIPRPP01,RCT-6,1/6,8192,SIPRNET
NIPRPP02,RCT-6,2/6,4096,NIPRNET
BSPEPP03,RCT-6,1/10,472,BSPE
DPV0PP04,1/10,2/6,472,DPV0
SIPRPP04,1/6,1/3,8192,SIPRNET
NIPRPP03,1/6,1/10,8192,NIPRNET
SIPRPP03,3/6,1/10,4096,NIPRNET
NIPRZP01,MEB,RCT-6,8192,NIPRNET
SIPRZP01,MEB,RCT-6,4096,SIPRNET
,並寫入到這個新文件:
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.
我想組一起陳述基於whic h任務是針對哪些單元的 - 我通過在第一列中寫入相關單元來完成此任務,然後我可以通過在Excel中手動排序將相關任務分組到每個單元。
有沒有辦法讓程序在將數據寫入新的csv時對數據進行排序?或者有沒有辦法,我可以這樣組合他們:
1/6
Terminate...
Establish...
2/6
Terminate...
Establish...
Establish...
3/6
Terminate...
Terminate...
我欣賞任何幫助,我可以得到與此。
謝謝,這看起來不錯。但是,當我嘗試運行它時,我得到了第17行'outf.write(up.format(* v))'中的「IndexError:元組索引超出範圍」。 是因爲在up語句中有5個變量,而在down語句中有4個變量? – nort0
啊是啊,'V'應該是'I' –