By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,705 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,705 IT Pros & Developers. It's quick & easy.

csv columns into variables

P: 2
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!

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. import csv
  3. a = [];
  4. i=0;
  5. b='';
  6. csvReader = csv.reader(open('sample.csv', 'rb'), delimiter='|');
  7. for row in csvReader:
  8.     a.append(row);
  9.  
  10. for i in range(0, len(a)):
  11.     print a[i];
Jun 10 '12 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 626
Try this and post back if there is further confusion with some test data, as the format of the data is the limiting factor.
Expand|Select|Wrap|Line Numbers
  1. import csv
  2. a = [];
  3. csvReader = csv.reader(open('sample.csv', 'rb'), delimiter='|');
  4. for row in csvReader:
  5.     a.append(row);
  6.  
  7. for row in a:
  8.     for column in row:
  9.         print column, "***",
  10.     print 
Jun 10 '12 #2

P: 2
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!
Jun 12 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.