Python: My csv.reader imports csv file as text
Question: After importing `CSV` file, the output appears like `[['1', '2', '3']]`. How
can I make sure it is imported as a number?


Answer: With the csv module `reader` function a row read from the csv file is returned
as a list of strings. If you want unquoted numbers not to be returned as
strings use the `quoting=csv.QUOTE_NONNUMERIC` option. However, be aware that
this option will convert to float. If you want to process or display these
numbers as `int`s then you will have to cast them as `int`.

This is your csv file:

    
    
    1,2,3
    4,5,6
    7,8,9
    

Assuming you want a list of lists output:

    
    
    import csv                                                                                                                                    
    tst = open('test.csv')                                                                                                                          
    l = []                                                                                                                                          
    reader = csv.reader(tst, quoting=csv.QUOTE_NONNUMERIC)                                                                                         
    for line in reader:                                                                                                                           
        l.append(line)
    
    print l
    

Yields:

    
    
    [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]
    

To output integers, you might as well convert the strings to integers
directly:

    
    
    l = []                                                                                                                                          
    tst = open('test.csv')                                                                                                                          
    reader = csv.reader(tst)                                                                                                                      
    for line in reader:                                                                                                                           
        l.append([int(i) for i in line])                                                                                                          
    
    print l 
    

Yields:

    
    
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    

