python - Join Multiple Files Dictionary -
i have master table contains fields. want join bunch of other csvs.
current data looks like:
file 1:
key attrib1 attrib2 attrib3 attrib4
file 2:
key attrib5
file 3:
key attrib6
i want final output like:
key attrib1 attrib2 attrib3 attrib4 attrib5 attrib6, etc.
not files contain of keys.
current code:
master = "in.csv" file1 = "file.csv" file2 = "file2.csv" prime = list() d1 = {} open(master) f: k in csv.reader(f): prime.append(k[0]) k in prime: open(file1,'r') csvfile: rd = csv.reader(csvfile,delimiter=",") row in rd: if row[0] ==k: d1 = dict((row[0],row[1]) rows in rd) open(file2,'r') csvfile: rd = csv.reader(csvfile,delimiter=",") row in rd: if row[0] ==k: d1 = d1+dict((row[0],row[1]) rows in rd)
i think close if not want:
master = "in.csv" filelist = "file.csv", "file2.csv" joined = "joined.csv" dict1 = {} open(master, 'r') csvfile: row in csv.reader(csvfile): key = row[0] dict1[key] = row[1:] # note not check duplicate keys filename in filelist: open(filename, 'rb') csvfile: seen = set() row in csv.reader(csvfile): key = row[0] if key in dict1: if key in seen: print('error: duplicate key %r in file %r - ignored' % (key, filename)) else: dict1[key].append(row[1]) seen.add(key) else: # key not in master pass # ignore # add null entry keys not present in file key in dict1: if key not in seen: dict1[key].append(none) # write data in merged dictionary new csv file open(joined, 'wb') newcsvfile: csv.writer(newcsvfile).writerows( ([key]+attrlist) key, attrlist in sorted(dict1.iteritems()))
Comments
Post a Comment