Python csv processing
Question: i have an python exercise that require write a program that extract
information from 5 csv file name QLD2010.csv, QLD2011.csv, QLD2012.csv,
QLD2013.csv and QLD2014.csv In each file there some data like that:

    
    
    Girl Names,Count of Girl Names,Boy Names,Count of Boy Names
    Ruby,440,Cooper,493
    Mia,425,William,493
    Isabella,401,Jack,452
    

And i have to merge data from 5 file to a new csv file with this format:

    
    
    Year,Babyname,Count of names,Gender
    

eg.

    
    
    2010,Harper,54,Girl
    2010,Hunter,195,Boy
    

I have no idea how to make the 'year' from file name and how to merge girl
name and boy name in one row and show the gender. Anyone know how to do that?
Thanks!


Answer: If the filenames all have the same format, you can use slicing to get the
years easily;

    
    
    In [1]: filenames = 'QLD2010.csv QLD2011.csv QLD2012.csv QLD2013.csv QLD2014.csv'.split()
    
    In [2]: filenames
    Out[2]: ['QLD2010.csv', 'QLD2011.csv', 'QLD2012.csv', 'QLD2013.csv', 'QLD2014.csv']
    
    In [4]: [fn[3:7] for fn in filenames]
    Out[4]: ['2010', '2011', '2012', '2013', '2014']
    

Or you might want to put them into a dictionary for easy access later;

    
    
    In [5]: {fn: int(fn[3:7]) for fn in filenames}
    Out[5]: 
    {'QLD2010.csv': 2010,
     'QLD2011.csv': 2011,
     'QLD2012.csv': 2012,
     'QLD2013.csv': 2013,
     'QLD2014.csv': 2014}
    

If the filenames are not so uniform, you could use regular expressions. The
expression `(\d{4})` basically means: match _exactly_ four digits, and return
the match as a group.

    
    
    In [6]: import re
    
    In [7]: {fn: int(re.search('(\d{4})', fn).group()) for fn in filenames}
    Out[7]: 
    {'QLD2010.csv': 2010,
     'QLD2011.csv': 2011,
     'QLD2012.csv': 2012,
     'QLD2013.csv': 2013,
     'QLD2014.csv': 2014}
    

With regard to processing the lines from the CSV file, it is not that
difficult to split them up, assuming each line has the same form;

    
    
    In [8]: 'Ruby,440,Cooper,493'.split(',')
    Out[8]: ['Ruby', '440', 'Cooper', '493']
    

The easiest way to store the data is in a dictionary;

    
    
    In [18]: boys, girls = {}, {}
    
    In [19]: girls[row[0]] = int(row[1])
    
    In [20]: boys[row[2]] = int(row[3])
    
    In [21]: boys
    Out[21]: {'Cooper': 493}
    
    In [22]: girls
    Out[22]: {'Ruby': 440}
    

