各位大大好,请先看一段小小程式码,如下:
for x in range(1,1001,1):
with open('fix.csv','rb') as csvinput:
with open('fix_append.csv','wb') as csvoutput:
writer = csv.writer(csvoutput)
for row in csv.reader(csvinput):
if j<len(AddNewColumn):
writer.writerow([row[0],row[1],...,row[x],AddNewColumn[j]])
以上就是想把fix.csv的东西写进fix_append.csv,然后再加上一列AddNewColumn的值
以下为小弟问题,请问各位大大
如何将writer.writerow([row[0],row[1],...,row[x],AddNewColumn[j]])的row[x]设定
为变量呢?
也就是说,在最上层的for循环里,
当x=1,writer.writerow([row[0],AddNewColumn[j]])
当x=2,writer.writerow([row[0],row[1],AddNewColumn[j]])
当x=3,writer.writerow([row[0],row[1],row[3],AddNewColumn[j]])
小弟有尝试用字串的方式来解,如下
for x in range(1,1001,1):
xxx="%d"%(x-1)
RowString=RowString+'row['+xxx+'],'
with open('fix.csv','rb') as csvinput:
with open('fix_append.csv','wb') as csvoutput:
writer = csv.writer(csvoutput)
for row in csv.reader(csvinput):
if j<len(AddNewColumn):
Astring= RowString + "AddNewColumn["+ "%d"%j +"]"
Astring=Astring.split(',')
writer.writerow(Astring)
用上述方式印在fix_append.csv表上的就不是fix.csv里每一行row的值了,而是单纯的字
串row[0]、...、row[x]、AddNewColumn[j]
麻烦各位程式大神解惑了,感谢!