0

私のコードはほとんど機能しており、私は夢中になっています。csvファイルを取得し、行に番号を付け、列内のすべてのフィールドの文字数を見つけ、これらすべてを新しいcsvに書き込み、元のcsvファイルから他のすべての行を新しいcsvにコピーするコードがあります. (それが理にかなっていることを願っています)。

私のコード:

from csv import DictReader, DictWriter


with open('file1') as fil1,\ # Original file
     open('file2') as fil2,\ # File 2 (for line num and length headers)
     open('file3', 'wb') as fcompout,

# file3 is new file (to be original file +: line numbers and length of 'Name')

    read1 = DictReader(fil1)
    read2 = DictReader(fil2)

    writenum = DictWriter(fcompout, fieldnames=read2.fieldnames)
    writenum.writeheader()


    num = 0 # Number value
    for line1 in read1:
        newdic=dict(line1)
        fil2.seek(0) # resets the reader1 iterator
        for line2 in read2:
            num=num+1 # Number for line num
            outline1=dict(line2)
            outline1['Name']=newdic['Name']
            outline1['Length']=len(newdic['Name']) # Length of Name
            outline1['Line Num']=num # Line Numbers
            outline1['Geo Location']=newdic['Geo Location']
            outline1['IPStart']=newdic['IPStart']
            outline1['IPStop']=newdic['IPStop']
            outline1['Command']=newdic['Command']
            outline1['file']=newdic['file']
            outline1['NA']=newdic['NA']
            outline1['Default']=newdic['Default']
            writenum.writerow(outline1) # Write line to new csv

元のファイル (file1):

Command,Name,file,Geo Location,NA,Default,IPStart,IPStop
,DRE EXT Ascent Media,,,,,,
,DRE United Kingdom UKLONEXT LON Extrane lo0.UKLONEXT sw01,,United Kingdom,,,,
,DRE United Kingdom UKLONEXT LON Extrane lo0.UKLONEXT sw02,,United Kingdom,,,,
,DRE United Kingdom UKLONDRE LON Sq lab dynamips test nets,,United Kingdom,,,,
,DRE United States USLAXCPT Corp Point Multilink_Ascent Media,,United States,,,,
,DRE United Kingdom UKLONSDD SASD-D CORE01 to SW01,,United Kingdom,,,,
,DRE United Kingdom UKLONSDD SASD-D CORE01 to SW02,,United Kingdom,,,,
,DRE United Kingdom UKLONSDD SASD-D CORE02 to SW01,,United Kingdom,,,,
,DRE United Kingdom UKLONSDD SASD-D CORE02 to SW02,,United Kingdom,,,,
,DRE United Kingdom UKLONSDD SASD-D CORE01 to CORE02,,United Kingdom,,,,
,DRE United Kingdom UKLONSDD SASD-D SW01 to SW02,,United Kingdom,,,,
,DRE United States USLAXMDR SASD-D XC VRF T1/7 Xconnect to vrf via Te1/7,,United States,,,,
,DRE Hong Kong (China) HKHKGEXT Hongkong Extranet HKHKGDRE SW01 interconnect,,"Hong Kong, Hong Kong",,,,
,DRE United Kingdom UKLONDRE LON Sq lab test nets,,United Kingdom,,,,
,DRE United States USLAXTHA SPS Thalberg uslaxtha mdf01 Interconnect,,United States,,,,
,DRE Hong Kong (China) HKHKGDRE SASD-D Citiplaza to EXT Interconnect,,Hong Kong,,,,
,SASD-D United States USPHXCAP VRF SW02,,United States,,,,

ヘッダー/新しい列 (file2) の場合:

Line Num,Command,Name,Length,file,Geo Location,NA,Default,IPStart,IPStop

新しいファイル (file3):

Line Num,Command,Name,Length,file,Geo Location,NA,Default,IPStart,IPStop
1,,DRE United Kingdom UKLONEXT LON Extrane lo0.UKLONEXT sw01,57,,United Kingdom,,,,
2,,DRE United Kingdom UKLONEXT LON Extrane lo0.UKLONEXT sw02,57,,United Kingdom,,,,
3,,DRE United Kingdom UKLONDRE LON Sq lab dynamips test nets,57,,United Kingdom,,,,
4,,DRE United States USLAXCPT Corp Point Multilink_Ascent Media,60,,United States,,,,
5,,DRE United Kingdom UKLONSDD SASD-D CORE01 to SW01,49,,United Kingdom,,,,
6,,DRE United Kingdom UKLONSDD SASD-D CORE01 to SW02,49,,United Kingdom,,,,
7,,DRE United Kingdom UKLONSDD SASD-D CORE02 to SW01,49,,United Kingdom,,,,
8,,DRE United Kingdom UKLONSDD SASD-D CORE02 to SW02,49,,United Kingdom,,,,
9,,DRE United Kingdom UKLONSDD SASD-D CORE01 to CORE02,51,,United Kingdom,,,,
10,,DRE United Kingdom UKLONSDD SASD-D SW01 to SW02,47,,United Kingdom,,,,
11,,DRE United States USLAXMDR SASD-D XC VRF T1/7 Xconnect to vrf via Te1/7,71,,United States,,,,
12,,DRE Hong Kong (China) HKHKGEXT Hongkong Extranet HKHKGDRE SW01 interconnect,75,,"Hong Kong, Hong Kong",,,,
13,,DRE United Kingdom UKLONDRE LON Sq lab test nets,48,,United Kingdom,,,,
14,,DRE United States USLAXTHA SPS Thalberg uslaxtha mdf01 Interconnect,67,,United States,,,,
15,,DRE Hong Kong (China) HKHKGDRE SASD-D Citiplaza to EXT Interconnect,67,,Hong Kong,,,,
16,,SASD-D United States USPHXCAP VRF SW02,38,,United States,,,,

上記の file3 は、私のコードからの現在の出力です。file1 の最初の行が欠落していることに注意してください。別の行を最初に作成するように順序を変更しても問題ありません。常に最初の行が欠落しており、他のすべての行は完璧に見えます。私はそれを理解することはできません。

助けてください...

ありがとう、B0T

4

1 に答える 1

1

あなたがそこで何をしているのかよくわかりませんが、はるかに簡単です:

from csv import DictReader, DictWriter

with open('file1') as fil1, open('file2') as fil2, open('file3', 'wb') as fcompout:
    read1 = DictReader(fil1)
    read2 = DictReader(fil2)
    writenum = DictWriter(fcompout, fieldnames=read2.fieldnames)
    writenum.writeheader()

    for num, line in enumerate(read1):
        line['Length'] = len(line['Name']) # Length of Name
        line['Line Num'] = num+1           # Line Numbers
        writenum.writerow(line)            # Write line to new csv

これらの値をすべて再割り当てする必要はありません。必要なものを追加するだけです。

結果:

Line Num,Command,Name,Length,file,Geo Location,NA,Default,IPStart,IPStop
1,,DRE EXT Ascent Media,20,,,,,,
2,,DRE United Kingdom UKLONEXT LON Extrane lo0.UKLONEXT sw01,57,,United Kingdom,,,,
3,,DRE United Kingdom UKLONEXT LON Extrane lo0.UKLONEXT sw02,57,,United Kingdom,,,,
4,,DRE United Kingdom UKLONDRE LON Sq lab dynamips test nets,57,,United Kingdom,,,,
5,,DRE United States USLAXCPT Corp Point Multilink_Ascent Media,60,,United States,,,,
6,,DRE United Kingdom UKLONSDD SASD-D CORE01 to SW01,49,,United Kingdom,,,,
7,,DRE United Kingdom UKLONSDD SASD-D CORE01 to SW02,49,,United Kingdom,,,,
8,,DRE United Kingdom UKLONSDD SASD-D CORE02 to SW01,49,,United Kingdom,,,,
9,,DRE United Kingdom UKLONSDD SASD-D CORE02 to SW02,49,,United Kingdom,,,,
10,,DRE United Kingdom UKLONSDD SASD-D CORE01 to CORE02,51,,United Kingdom,,,,
11,,DRE United Kingdom UKLONSDD SASD-D SW01 to SW02,47,,United Kingdom,,,,
12,,DRE United States USLAXMDR SASD-D XC VRF T1/7 Xconnect to vrf via Te1/7,71,,United States,,,,
13,,DRE Hong Kong (China) HKHKGEXT Hongkong Extranet HKHKGDRE SW01 interconnect,75,,"Hong Kong, Hong Kong",,,,
14,,DRE United Kingdom UKLONDRE LON Sq lab test nets,48,,United Kingdom,,,,
15,,DRE United States USLAXTHA SPS Thalberg uslaxtha mdf01 Interconnect,67,,United States,,,,
16,,DRE Hong Kong (China) HKHKGDRE SASD-D Citiplaza to EXT Interconnect,67,,Hong Kong,,,,
17,,SASD-D United States USPHXCAP VRF SW02,38,,United States,,,,
于 2013-07-16T21:26:13.890 に答える