Got Homework?
Connect with other students for help. It's a free community.
Here's the question you clicked on:
 0 viewing
paynito
Group Title
want to read rows from a csv file and turn them into lists
my code >>
list 0 = []
list 1 = []
list 2 = []
list 3 = []
list 4 = []
list 5 = []
with open('all_plans2.csv', 'rb') as csvfile:
myreader = csv.reader(csvfile)
# for row in myreader:
# print ', '.join(row)
count = 0
for row in myreader:
print count
count += 1
listcount =rowcount
this prints
0
1
2
3
4
5
so I have 6 rows in the csv that I would like to manipulate as 6 lists:(list0, list1, ...)
 2 years ago
 2 years ago
paynito Group Title
want to read rows from a csv file and turn them into lists my code >> list 0 = [] list 1 = [] list 2 = [] list 3 = [] list 4 = [] list 5 = [] with open('all_plans2.csv', 'rb') as csvfile: myreader = csv.reader(csvfile) # for row in myreader: # print ', '.join(row) count = 0 for row in myreader: print count count += 1 listcount =rowcount this prints 0 1 2 3 4 5 so I have 6 rows in the csv that I would like to manipulate as 6 lists:(list0, list1, ...)
 2 years ago
 2 years ago

This Question is Closed

andrew.m.higgs Group TitleBest ResponseYou've already chosen the best response.0
What happens if you print row?
 2 years ago

msmithhnova Group TitleBest ResponseYou've already chosen the best response.1
The first lines list 0 =[] produce errors when I try them. The import csv is missing. The listcount=rowcount row gives an error because they are not used at all other than that line. The program above if all were working would print exactly what you ask it to, the integer variable count. Since there are 6 rows it would print the integers fro 0 to 6 not including 6. Below is an example that uses a list of list and a dictionary of lists. Which would be easier and much better than using separate list variables, and will work with any number of lines without adding more variables. import csv # initialize list rows1 = [] # initialize dictionary rows2 = {} with open('f:/test.csv', 'rb') as csvfile: myreader = csv.reader(csvfile) count = 0 for row in myreader: # add row as list to list rows1.append(row) # add row as list to dictionary rows2[count] = row count += 1 print rows1 print print rows2
 2 years ago

paynito Group TitleBest ResponseYou've already chosen the best response.0
Thank you, that's what i needed. I appreciate you taking the time to help me.
 2 years ago
See more questions >>>
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.