Import csv as list in python
Question: I am trying to import a csv file as a list:

    
    
    file = open('curr.csv', 'rt')
    f = file.read()
    f = f.split(',')
    
    print(f)
    

The csv file is only 'GBP, USD' so I want the list ['GBP', 'USD']. However the
result I get is:

    
    
    ['GBP', 'USD\n']
    

How do I stop \n from appearing on the last value?


Answer: You need to strip your lines,but as a pythonic way you can use [`csv`
module](https://docs.python.org/2/library/csv.html) for dealing with `csv`
files :

    
    
    >>> import csv
    >>> with open('curr.csv', 'rb') as csvfile:
    ...     spamreader = csv.reader(csvfile, delimiter=',')
    ...     print list(spamreader)
    

Note that this will return a nested list of your `csv` file rows if you just
want the firs row in a list you can use `next()` method of `reader` object :

    
    
    >>> import csv
    >>> with open('curr.csv', 'rb') as csvfile:
    ...     spamreader = csv.reader(csvfile, delimiter=',')
    ...     print spamreader.next() 
    

And if you want the whole of your rows within a list you can use a nested list
comprehension :

    
    
    >>> import csv
    >>> with open('curr.csv', 'rb') as csvfile:
    ...     spamreader = csv.reader(csvfile, delimiter=',')
    ...     print [j for i in spamreader for j in i]
    

