Q: Joining two csv files after I've used dictreader I'm new to Python and am trying to take about 30 csv files, all with the same headers, but with different data in each file and merge in to one.
For now I'm pretending I only have two files to take in, one with info on ID numbers 1,2 and 3 and the other with info on 4,5 and 6
So far I've managed to take the files in and use dictreader to create a bunch of dictionaries, with the same keys, but I can't work out how to take the values that correspond to each key and join them on one big file with all the data together.
Here's how far I've gotten so far :)
for filename in os.listdir(os.getcwd()):
  root, ext = os.path.splitext(filename)
  if root.startswith('file1') and ext == '.csv':
      upload1 = filename
  if root.startswith('file2') and ext == '.csv':
      upload2 = filename

file_upload1 = open(upload1, 'rU')
data_upload1 = csv.DictReader(file_upload1)

for row in data_upload1:
  print row

And the same for upload2. And then I get an output like this;
{'ID': '1', 'name': 'bert', 'age': '30'}
{'ID': '2', 'name': 'ernie', 'age': '40'}
{'ID': '3', 'name': 'pinky', 'age': '50'}
{'ID': '4', 'name': 'perky', 'age': '60'}
etc

And what I would love to make is one csv file with one column for IDs 1,2,3,4,5,6 and then a column for each corresponding name, and each corresponding age etc.
Can someone please suggest how I could achieve this?
Sorry for the rookie question.

A: You'll get a generator of dicts when you call csv.DictReader(). Say we have 2 files: "f1.csv" and "f2.csv" and each of them contains multiple records. 

    import csv

    fp1, fp2 = open("f1.csv"), open("f2.csv")
    ld1, ld2 = csv.DictReader(fp1), csv.DictReader(fp2) #ld1 and ld2 are *generators*

    res = []  #our result will be stored here(LIST of dicts)
    ds = [ld1, ld2]    #list of generators

then, iterate over ds, while adding each k-v pair from each dict to the res dict, like so:
for d in ds: #each d is a generator
    for e in d: #each e is a dict
        res.append(e)

This res is a list now which contains dicts from both the sources. Just iterate over it and write them.
Note that you can make the code more robust/efficient, but first just understand the method :)
regards,
Yati Sagade
