Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

paynito

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, ...)

  • one year ago
  • one year ago

  • This Question is Closed
  1. andrew.m.higgs
    Best Response
    You've already chosen the best response.
    Medals 0

    What happens if you print row?

    • one year ago
  2. msmithhnova
    Best Response
    You've already chosen the best response.
    Medals 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

    • one year ago
  3. paynito
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank you, that's what i needed. I appreciate you taking the time to help me.

    • one year ago
    • Attachments:

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.