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

新文件(文件 3):

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 回答