How to read csv file with string but convert to different type and send to an array in python?
Question: I have a csv file, contain datetime, number1, number2, number3 number4.

I use code to read, but how to change the types.

my code:

    
    
    import csv
    import datetime
    myarray=([])
    filename='Contract.csv'
    f=csv.reader(open(filename,'rb'), delimiter=',')
    for row in f:
        myarray=array([row for row in f])
    print myarray
    

I get the array looks like: [['2010-05-01 15:20:12 0000' '345' '234' '163'
'120'], ['2010-05-02 15:22:12 0000' '335' '214' '164' '120'], ... ]

I have no idea how to change the first column into datetime and the others
into float. Please help. Thanks


Answer: First, [read this question](http://stackoverflow.com/questions/354038/how-do-
i-check-if-a-string-is-a-number-in-python).

Having said that. I use a function like this one (requries [python-
dateutil](http://labix.org/python-dateutil)) to manage dates too:

    
    
    from dateutil.parser import parse as date_parser
    def _cast_value(self, value):
        tests = (
            int,
            float,
            lambda value: date_parser(value)
        )
        for test in tests:
            try:
                return test(value)
            except ValueError:
                continue
        return value
    

`dateutil` will handle different kind of date formats for you.

