在CSV列中插入整数列表于其他数据后
我想在一个CSV文件中插入一个新列,放在已有数据的后面。比如,我的CSV文件现在包含:
Heading 1 Heading 2
1
1
0
2
1
0
我有一个整数列表,格式是:
[1,0,1,2,1,2,1,1]
我该如何把这个列表插入到CSV文件的“Header 2”下面呢?
到目前为止,我只能做到把新的整数列表加在现有数据的下面,比如:
Heading 1 Heading 2
1
1
0
2
1
0
1
0
1
2
1
2
1
1
使用的代码是:
#Open CSV file
with open('C:\Data.csv','wb') as g:
#New writer
gw = csv.writer(g)
#Add headings
gw.writerow(["Heading 1","Heading 2"])
#Write First list of data to heading 1 (orgList)
gw.writerows([orgList[item]] for item in column)
#For each value in new integer list, add row to CSV
for val in newList:
gw.writerow([val])
3 个回答
1
未经测试:
import csv
from itertools import izip_longest
some_ints = [1,0,1,2,1,2,1,1]
with open('input') as fin, open('output', 'w') as fout:
csvin = csv.reader(fin)
csvout = csv.writer(fout)
cols = izip_longest(csvin, some_ints, fillvalue='')
csvout.writerows(cols)
1
你遇到了两个相关的问题:
- 你在读取的同时又在写入同一个文件
- 你只想输出新数据,而不是包含新旧数据的新行。
首先,把数据读到内存中:
with open(r'C:\Data.csv','rb') as f:
rows = list(csv.reader(f))
然后把元组列表转换成一个新的元组列表,里面包含新数据:
newdata = [1,0,1,2,1,2,1,1]
newrows = [rows[0]] # keep the current two headers
newrows += izip_longest(rows[1:], newdata, fillvalue=0)
最后,把数据写回到磁盘上:
with open(r'C:\Data.csv', 'wb') as f:
csv.writer(f).writerows(newrows)
1
这里有一段代码,可以修复这个错误:
import csv
from itertools import izip_longest
# Creating a CSV file
with open(r'Data.csv','wb') as f:
fw = csv.writer(f)
fw.writerows( (('Heading 1', 'Heading 2'),
('1'),
('1'),
('0'),
('2'),
('1'),
('0')) )
print "The CSV file at start, read by a csv.reader :\n"
with open(r'Data.csv','rb') as f:
fr = csv.reader(f)
print '\n'.join(map(repr,fr))
print '\n------------------------------------------'
newdata = [10,0,10,20,10,20,10,10]
with open(r'Data.csv','rb') as f:
fr = csv.reader(f)
newrows = [fr.next()]
newrows += (a+[b] for a,b in izip_longest(fr, newdata,
fillvalue=[0]))
print 'newrows\n',newrows
with open(r'Data.csv', 'wb') as f:
csv.writer(f).writerows(newrows)
print '------------------------------------------\n'
print "The new CSV file created, read by a csv.reader :\n"
with open(r'Data.csv','rb') as f:
fr = csv.reader(f)
print '\n'.join(map(repr,fr))
它会显示:
The CSV file at start, read by a csv.reader :
['Heading 1', 'Heading 2']
['1']
['1']
['0']
['2']
['1']
['0']
------------------------------------------
newrows
[['Heading 1', 'Heading 2'], ['1', 10], ['1', 0], ['0', 10], ['2', 20], ['1', 10], ['0', 20], [0, 10], [0, 10]]
------------------------------------------
The new CSV file created, read by a csv.reader :
['Heading 1', 'Heading 2']
['1', '10']
['1', '0']
['0', '10']
['2', '20']
['1', '10']
['0', '20']
['0', '10']
['0', '10']
编辑
更加简洁
import csv
from itertools import izip_longest
from os import remove,rename
# Creating a CSV file
with open(r'Data.csv','wb') as f:
fw = csv.writer(f)
fw.writerows( (('Heading 1', 'Heading 2'),
('1'),
('1'),
('0'),
('2'),
('1'),
('0')) )
print "The CSV file at start, read by a csv.reader :\n"
with open(r'Data.csv','rb') as f:
print '\n'.join(map(repr,csv.reader(f)))
#------------------------------------------
newdata = [10,0,10,20,10,20,10,10]
with open(r'Data.csv','rb') as f, open(r'newData.csv','wb') as g:
fr = csv.reader(f)
gw = csv.writer(g)
gw.writerow(fr.next())
gw.writerows( a+[b] for a,b in izip_longest(fr, newdata,
fillvalue=[0]) )
remove (r'Data.csv')
rename (r'newData.csv',r'Data.csv')
#------------------------------------------
print "The new CSV file created, read by a csv.reader :\n"
with open(r'Data.csv','rb') as f:
print '\n'.join(map(repr,csv.reader(f)))