Python Forum
Iterate through csv file starting from row 2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterate through csv file starting from row 2
#1
I'm fairly new to programming but I'm trying to write a program that iterates through the length of a csv file row by row. It will create 3 new csv files and write data from the source csv file to each of the new csv files. The program does this for the entire row length of the csv file.

For the first if statement, I want it to copy every third row starting at the first row and save it to a new csv file(the next rows it copies would be row 4, row 7, row 10, etc)

For the second elif statement, I want it to copy every third row starting at the second row and save it to a new csv file(the next row it copies would be row 5, row 8, row 11, etc).

For the third elif statement, I want it to copy every third row starting at the third row and save it to a new csv file(the next row it copies would be row 6, row 9, row 12, etc).


The first if statement does and works exactly the way I want it to whereas the first and second elif statements don't. They both start from the very first row and I'm trying to get them to start iterating from the second and third rows.

Here is my code:

import pandas as pd
from itertools import islice
import csv

Sourcedataframe = pd.read_csv("blankAgentList.csv", header=None)
count = 1
count2 = 0

for index, row in Sourcedataframe.iterrows(): #going through each row line by line

    #this for loop counts the amount of times it has gone through the csv file. If it has gone through it more than three times, it resets the counter back to 1.
    for column in Sourcedataframe: 
        if count > 3:
            count = 1

            #if program is on it's first count, it opens the 'Sourcedataframe', reads/writes every third row to a new csv file named 'agentList1.csv'.
        if count == 1:
            with open('blankAgentList.csv') as infile: 
                
              with open('agentList1.csv', 'w') as outfile:
                reader = csv.DictReader(infile)
                writer = csv.DictWriter(outfile, fieldnames=reader.fieldnames)
                writer.writeheader()
                for row in reader:
                    count2 += 1
                    if not count2 % 3:
                        writer.writerow(row)
            
        elif count == 2:
            with open('blankAgentList.csv') as infile:
              
              with open('agentList2.csv', 'w') as outfile:
                reader = csv.DictReader(infile)
                writer = csv.DictWriter(outfile, fieldnames=reader.fieldnames)
                writer.writeheader()
                for row in reader:
                    count2 += 1
                    if not count2 % 3:
                        writer.writerow(row)
            
        elif count == 3:
            with open('blankAgentList.csv') as infile:
                
              with open('agentList3.csv', 'w') as outfile:
                reader = csv.DictReader(infile)
                writer = csv.DictWriter(outfile, fieldnames=reader.fieldnames)
                writer.writeheader()
                for row in reader:
                    count2 += 1
                    if not count2 % 3:
                        writer.writerow(row)
            
        count = count + 1 #counts how many times it has ran through the main for loop. 
How can I change this so that the first and second elif statements start iterating from the second and third rows and skips every third row?
Reply
#2
You can run all three concurrently:
# open the source file here, once only
source = open('agentList2.csv', 'w')
reader = csv.DictReader(source)
# create each output file here
writer_1 = ...
writer_2 = ...
writer_3 = ...
count = 0
for line in reader:
    if count == 0:
        # do the first line thing
    elif count == 1:
        # do the second line thing
    elif count == 2:
        # do the third line thing
    elif count % 3 = 0:
        # every third for first
    elif count % 3 = 1:
        # every third for second
    else:
        # every third for third
    count += 1
Reply
#3
Actually you can use the fact that row_number%3follows certain pattern.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,827 Aug-10-2020, 11:01 PM
Last Post: medatib531

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020