mcfadX
mcfadX

Reputation: 83

Reading and writing to a csv using iteration

Hello I am having some difficulties with my code. I'm trying to read a csv, append some values and then write to its own csv file. I have a list of lists that I'm trying to compare all the contents in each list together. However I am experiencing some problems. When I am reading the csv to get values, it is not iterating over the lists in order, as it is taking some items from both lists. It starts by iterating over the second list as well. For example: group = [[Walmart, Target],[Kohls, Macys]], will print: Macys, Walmart, Kohls, Target. And then when I write to the csv, it's putting all the items together in the same cell.

My Code:

group = [[Walmart, Target],[Kohls, Macys]]
name = []
q_one = []
q_two = []
q_three = []
q_four = []
count = 0

with open('practice.csv') as fh:
    spreadsheet = csv.DictReader(fh, delimiter=',')

    for row in spreadsheet:
        for list in group:
            for company in list:
                if str(company) == row['Name']:
                   q_one.append(row['Q1'])
                   q_two.append(row['Q2'])
                   q_three.append(row['Q3'])
                   q_four.append(row['Q4'])
                   name.append(row['Name'])

with open('companys.csv', 'w', newline='') as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=',')
    for y in name:
        if count = (count % 2) == 0:
           csvwriter.writerow(["Name", "Q1", "Q2", "Q3", "Q4"])
           csvwriter.writerow([name, q_one, q_two, q_three, q_four])
           count = count + 1
        if count = (count % 2) == 1:
           csvwriter.writerow([name, q_one, q_two, q_three, q_four])
           count = count + 1

Desired Output:

Name    Q1  Q2  Q3  Q4
Walmart ..  ..  ..  ..
Target  ..  ..  ..  ..

Name    Q1  Q2  Q3  Q4
Kohls   ..  ..  ..  ..
Macys   ..  ..  ..  ..

Upvotes: 0

Views: 53

Answers (2)

Ariel Catala Valencia
Ariel Catala Valencia

Reputation: 112

Another solution could using count as an index as I present you:

with open('companys.csv', 'w', newline='') as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=',')
    for y in name:
        if (count % 2) == 0:
           csvwriter.writerow(["Name", "Q1", "Q2", "Q3", "Q4"])
           csvwriter.writerow([name[count], q_one[count], q_two[count], q_three[count], q_four[count]])
           count = count + 1
        elif (count % 2) == 1:
           csvwriter.writerow([name[count], q_one[count], q_two[count], q_three[count], q_four[count]])
           count = count + 1

Upvotes: 1

Tim
Tim

Reputation: 3427

It's because you are writing the entire list q_one, etc for each cell. You have to iterate those along with name. The best way is to use zip. For example:

with open('companys.csv', 'w', newline='') as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=',')
    for y, q1, q2, q3, q4 in zip(name, q_one, q_two, q_three, q_four):
        if count = (count % 2) == 0:
           csvwriter.writerow(["Name", "Q1", "Q2", "Q3", "Q4"])
           csvwriter.writerow([name, q1, q2, q3, q4 ])
           count = count + 1
        if count = (count % 2) == 1:
           csvwriter.writerow([name, q1, q2, q3, q4 ])
           count = count + 1

Upvotes: 1

Related Questions