我正在尝试读取一个csv文件并将数据写入另一个文件。我正面临着一些问题。我想将3个单元格数据合并为一个并写入另一个csv文件。请使用csv输入文件和输出文件查看下面的代码。
import csv
# initialize with empty ints and dicts
name, date_, indus ,nike1, nike2, nike3, paid, _abbr =[],[],[],[],[],[],[],[]
with open('company_inf1.csv','rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
reader.next() #skip header
for row in reader:
name.append(row[0])
date.append(row[1])
indus.append(row[2])
nike1.append(row[3])
nike2.append(row[4])
nike3.append(row[5])
paid.append(row[6])
abbr.append(row[7])
cn = list(name)
date = list(date)
indus = list(indus)
inf1 = list(nike1)
inf2 = list(nike2)
inf3 = list(nike3)
paid = list(paid)
abr = list(abbr)
a = [(inf1), (inf2), (inf3)]
b = [int(i[0]) for i in a]
c = [int(i[1]) for i in a]
d = [int(i[2]) for i in a]
t =((b),(c),(d))
with open('test123.csv','w') as cfile:
writer = csv.writer(cfile, delimiter=',')
writer.writerow(['id','name', 'date','indus','nike', 'paid ', 'abbr'])
for i,x in enumerate(cn):
writer.writerow([i,x ,date[i],indus[i],t,paid[i],abr[i]])
我的Csv文件是
Name Date Indus nike1 nike nike3 Paid absr
XYZ 30-06-1987 Service 1 2 3 10000 abs
abc 01-02-1986 MAN 4 5 6 sasa sa
def 02-02-1985 Abc 7 8 9 saassas qw
jhk 01-02-2002 adads 10 11 12 saasas qwws
输出
id name date industry neki paid abbr
0 XYZ ######## Service ([1, 2, 3], [4, 5, 6], [7, 8, 9]) 10000 abs
1 abc ######## MAN ([1, 2, 3], [4, 5, 6], [7, 8, 9]) sasa sa
2 def ######## Abc ([1, 2, 3], [4, 5, 6], [7, 8, 9]) saassas qw
输出应为
id name date industry sector paid abbr
0 XYZ ######## Service ([1, 2,3]) 10000 abs
1 abc ######## MAN ([4, 5, 6]) sasa sa
2 def ######## Abc ([7, 8, 9]) saassas qw
任何人都可以帮助我吗?
答案 0 :(得分:2)
以下内容应该有效。请注意完全删除所有这些行:
a = [(inf1), (inf2), (inf3)]
b = [int(i[0]) for i in a]
c = [int(i[1]) for i in a]
d = [int(i[2]) for i in a]
t = ((b),(c),(d))
并添加了一行:
t = (map(int, (inf1[i], inf2[i], inf3[i])),) # merge three columns together
到for i,x in enumerate(cn):
循环。我还修了一些错误的变量。
import csv
# initialize with empty ints and lists
name, date_, indus, nike1, nike2, nike3, paid, _abbr = [[] for _ in xrange(8)]
with open('company_inf1.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
reader.next() # skip header
for row in reader:
name.append(row[0])
date_.append(row[1])
indus.append(row[2])
nike1.append(row[3])
nike2.append(row[4])
nike3.append(row[5])
paid.append(row[6])
_abbr.append(row[7])
cn = list(name)
date = list(date_)
indus = list(indus)
inf1 = list(nike1)
inf2 = list(nike2)
inf3 = list(nike3)
paid = list(paid)
abr = list(_abbr)
with open('test123.csv', 'w') as cfile:
writer = csv.writer(cfile, delimiter=',')
writer.writerow(['id', 'name', 'date', 'indus', 'nike', 'paid', 'abbr'])
for i,x in enumerate(cn):
t = (map(int, (inf1[i], inf2[i], inf3[i])),) # merge three cols together
writer.writerow([i, x, date[i], indus[i], t, paid[i], abr[i]])
我认为最好只读取输入文件并在一个for
循环内同时写输出文件,因为它更简单,无需读取所有数据并将其存储到内存中立刻(因此不会有这么多变数)。
这就是我的意思:
import csv
with open('company_inf1.csv', 'rb') as inf, open('test123.csv', 'w') as outf:
reader = csv.reader(inf, delimiter=',')
reader.next() # skip header
writer = csv.writer(outf, delimiter=',')
writer.writerow(['id', 'name', 'date', 'indus', 'nike', 'paid', 'abbr'])
for i, (name, date, indus, nike1, nike2, nike3, paid, abbr) in enumerate(reader):
t = (map(int, (nike1, nike2, nike3)),) # convert & merge together 3 cols
writer.writerow([i, name, date, indus, t, paid, abbr])
答案 1 :(得分:0)
这是一个简单的解决方案,它将所有nike
组合在一起并在一个循环内写入:
import csv
with open('company_inf1.csv','rb') as csvfile, open('out.csv', 'w') as outfile:
writer = csv.writer(outfile)
writer.writerow(['id','name', 'date','indus','nike', 'paid', 'abbr'])
reader = csv.reader(csvfile)
reader.next() # skip header
for id, (name, date, industry, nike1, nike2, nike3, paid, abbreviation) in enumerate(reader):
nike = '([{0},{1},{2}])'.format(nike1, nike2, nike3)
writer.writerow([id, name, date, industry, nike, paid, abbreviation])
一些注意事项: