从一个csv获取输入并写入另一个cs

2024-10-01 19:14:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我从一个csv中提取数据,得到两个不同的变量,它们是不同格式的位置,一个用于一个楼层,另一个用于多个楼层。我的代码拉数据的工作如预期,我有困难写入正确的行在第二个csv,数据正在写入,但它正在做这个字母的字母。你知道吗

import csv
infile = open('vlan_dev.csv', 'rU')
reader = csv.reader(infile)
outfile = open('testout.csv', 'w')
writer  = csv.writer(outfile)
used_header = False
for row in reader:
    # skip over the CSV header
    if not used_header:
        used_header = True
        continue

    #defining variables
    building = row[3]
    startfloor = row[4]
    endfloor = row[5]
    subnet = row[6]
    mask = row[10]
    location1 = building.replace(' ', '')+startfloor
    location2 = building.replace(' ', '')+startfloor+'-'+endfloor
    iprange = subnet+'/'+mask
    if (building != 'NETWORK CORE' and subnet.startswith('10.96.')):
        if startfloor == endfloor:
            print location1
            writer.writerow(location1)
        elif startfloor != endfloor:
            print location2
            writer.writerow(location2)
        location = location1 + location2
        print location

我为第二部分尝试了一些选项,但在正确书写时遇到了困难。上面的代码接受正确的输出,但每个单元格只写一个字母。我尝试过其他的选择,但不知所措。你知道吗


Tags: csv数据if字母readerusedwriterrow

热门问题