Reading and writing CSV files into a data structure suitable for Excel-style column/row manipulations
Question: So I am currently working on a web-application with a few other people for a
client, and we've hit a stumbling block. Basically we need to be able to
upload a CSV file in a specific layout - and the application will take that
CSV file and based on specific columns and their values, it will perform the
algorithm and calculations required.

The output would also be a downloadable CSV file. None of us have had
experience working with CSV in Python.

The layout of the CSV file is as follows: ID, Name, Address, Suburb, Postcode,
Email, Phone

I need to take the address fields and use that in a calculation to determine
how to get to the destination from their specific address. I would also need
to print the specific details related to that person as well.

**EDIT** Okay so basically, the CSV file will contain details about employees
and their relevant personal information. What our application does is takes
that information, and based on the employees address, will predict the most
optimised route for them to get to the destination. Basically how the hell do
I read CSV files and then write an algorithm based on a certain column/row to
perform my calculations required.


Answer: Reading a `.csv` is easy with the
[csv](https://docs.python.org/2/library/csv.html) standard library module.

A more efficient library that allows for better manipulation of `.csv` files
is [pandas](http://pandas.pydata.org/), you should consider playing around
with this one first.

For instance, given a csv file:

    
    
    csv = r"""col1,col2,col3,col4
              bar,20150301,homer,53
              foo,20150502,bart,102
              barfoo,20150201,lisa,13
              foobar,20150501,marge,97"""
    

We can operate on it with the `csv` module:

    
    
    import csv # built-in no need to install
    from StringIO import StringIO 
    
    with open(StringIO(csv), 'rb') as f:
        reader = csv.reader(f)
        for row in reader:
            # Do whatever you need
    

And, similarly, with pandas:

    
    
    import pandas as pnd # external, installation required
    
    # returns a dataframe, specify cols, index et cetera 
    df = pnd.read_csv(StringIO(csv),
        header=0,
        index_col=["col1", "col3"], 
        usecols=["col1", "col2", "col3"],
        parse_dates=["col2"])
    
    # do dirty things with it.
    

