Python 从一个csv获取输入并写入第二个csv
我从一个csv中提取数据,得到两个不同的变量,它们是不同格式的位置,一个用于单个楼层,另一个用于多个楼层。我的拉取数据的代码按预期工作,我在写入第二个csv中的正确行时遇到问题,数据正在写入,但它正在逐字写入Python 从一个csv获取输入并写入第二个csv,python,csv,Python,Csv,我从一个csv中提取数据,得到两个不同的变量,它们是不同格式的位置,一个用于单个楼层,另一个用于多个楼层。我的拉取数据的代码按预期工作,我在写入第二个csv中的正确行时遇到问题,数据正在写入,但它正在逐字写入 import csv infile = open('vlan_dev.csv', 'rU') reader = csv.reader(infile) outfile = open('testout.csv', 'w') writer = csv.writer(outfile) used_
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
我已经尝试了第二部分的一些选项,但在正确书写时遇到了困难。上面的代码获取正确的输出,但每个单元格只写入一个字母。我尝试过其他的选择,但我不知所措
writerow
需要一个iterable,字符串是逐字符迭代的。尝试:
writer.writerow([location2])
如果我想写入第[2]行而不是第一行,我将如何编辑命令以更改写入位置。@Waitkus您必须包含两个空白列,例如
[“”,“”,location2]