샘플 데이터 :병합하는 방법 행, CSV
id, Name, mail, data1, data2, data3 1, Name1, [email protected], abc, 14, de 1, Name1, [email protected], fgh, 25, kl 1, Name1, [email protected], mno, 38, pq 2, Name2, [email protected], abc, 14, d
내가 첫 번째 필드를 선택하는 스크립트를 썼다는 중복을 취소 할 수있는 고유 한 문자열입니다. 그러나 필드 date1-3의 데이터는 반복되지 않으므로 결과를 작성해야합니다.
1, mail1 com, "abc, 14, de, fgh, 25, kl, mno, 38, pq "
배열의 행을 병합하는 방법은 무엇입니까? 내 코드는 작동하지 :
import sys
import csv
in_fln = sys.argv[1]
# You can replace here and choose any delimiter:
csv.register_dialect('dlm', delimiter=',')
csv.register_dialect('dmt', delimiter=';')
# if this .csv file do:
if (in_fln[-3:]) == "csv":
out_fln = 'out' + in_fln
inputf = open(in_fln, 'r')
seen = []
outfile = []
nout = {}
#rowun = []
try:
reader = csv.reader(inputf, dialect='dlm')
# select by ContactID
for row in reader:
if row[0] not in seen:
#IT'S work byt temp comment
#rowun = '"' + (row[-4]) + ', ' + (row[-3]) + ', ' + (row[-2]) + '"'
#outfile.append(row[:-5]+[rowun])
outfile.append(row[:-4])
rowun = (row[0])
nout[rowun] = (row[-4:-1])
seen.append(row[0])
print (type(row))
else:
#rowun = '"' + (row[-4]) + ', ' + (row[-3]) + ', ' + (row[-2]) + '"'
#nout.insert(-1,(row[-4:-1]))
print (type(row))
rowun = (row[0])
rowun2 = {rowun:(row[-4:-1])}
nout.update(rowun2)
finally:
#print (nout)
#print (outfile[:-1])
#csv.writer(open(('nout' + in_fln), 'w', newline='')).writerows(nout)
csv.writer(open(out_fln, 'w', newline=''), dialect='dlm').writerows(outfile)
inputf.close()
print ("All done")