csv columns into variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tralynn
    New Member
    • Jun 2012
    • 2

    csv columns into variables

    hello,

    I have a csv file that has 13 columns. Some columns are filled, some are blank. What I need to do is pull up the csv file, read the columns into a list or array and then put each column into its own variable that can be referenced in an email script that I've already written.

    Here is my code so far where my output is all of my columns but I'm stuck after this. Any help would be greatly appreciated!

    Code:
    #!/usr/bin/python
    import csv
    a = [];
    i=0;
    b='';
    csvReader = csv.reader(open('sample.csv', 'rb'), delimiter='|');
    for row in csvReader:
    	a.append(row);
    	
    for i in range(0, len(a)):
    	print a[i];
    Last edited by bvdet; Jun 19 '12, 02:57 AM. Reason: Add code tags
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Try this and post back if there is further confusion with some test data, as the format of the data is the limiting factor.
    Code:
    import csv
    a = [];
    csvReader = csv.reader(open('sample.csv', 'rb'), delimiter='|');
    for row in csvReader:
        a.append(row);
    
    for row in a:
        for column in row:
            print column, "***",
        print

    Comment

    • tralynn
      New Member
      • Jun 2012
      • 2

      #3
      Thank you!

      This works to get my data to print. It is just cells filled with names and class names and email addresses. I want to use each field and format an email with them to send with another script. So right now I have this printing out each of my fields with a separator but how can I assign each 13 columns of the csv file with its own variable into the list a? Fname, Lname, email, course1, course2, etc....
      I hope I'm making sense. I think it has something to do with the a.append(row) am i right? Can I append [0] as fname, [1] as lname and so on and so forth? and if so, how do I go about doing this?

      Thank you thank you for all your help!

      Comment

      Working...