Home > Net >  Loop over two csv files and check each row
Loop over two csv files and check each row

Time:04-13

I have two csv files and I'm attempting to loop over one to get the value of the row and then match that row up against another file and check each row. In example:

'design_products_view.csv'

"id","name","price","product","thumbnail","thumbnail_url","template","description","active_description","stages","variations","attributes","printings","order","active","author","created","updated","template_id"
15,"1.0' Circle",0,0,"thumbnails/lumise-media-01_round-curled-sticker-PacerPlaceholder-12.png","https://design.pacerlabels.com/data/thumbnails/eRsp0afJYlN2.png","","Available in White BOPP - Clear BOPP - Silver Metallic BOPP - Holographic Metallic BOPP and Kraft.",0,"JTdCJTIyVHlONUJiSHVRcSUyMiUzQSU3QiUyMmVkaXRfem9uZSUyMiUzQSU3QiUyMmhlaWdodCUyMiUzQTM3OSUyQyUyMndpZHRoJTIyJTNBMzc5JTJDJTIybGVmdCUyMiUzQTAuNSUyQyUyMnRvcCUyMiUzQTAuNSUyQyUyMnJhZGl1cyUyMiUzQSUyMjEwMCUyMiU3RCUyQyUyMnVybCUyMiUzQSUyMnByb2R1Y3RzJTJGbDBuMHA5Y3Muc3ZnJTIyJTJDJTIyc291cmNlJTIyJTNBJTIydXBsb2FkcyUyMiUyQyUyMm92ZXJsYXklMjIlM0FmYWxzZSUyQyUyMnByb2R1Y3Rfd2lkdGglMjIlM0E0MDAlMkMlMjJwcm9kdWN0X2hlaWdodCUyMiUzQTQwMCUyQyUyMnRlbXBsYXRlJTIyJTNBJTdCJTdEJTJDJTIyc2l6ZSUyMiUzQSU3QiUyMndpZHRoJTIyJTNBJTIyMS4xMjUlMjIlMkMlMjJoZWlnaHQlMjIlM0ElMjIxLjEyNSUyMiUyQyUyMmNvbnN0cmFpbiUyMiUzQXRydWUlMkMlMjJ1bml0JTIyJTNBJTIyaW5jaCUyMiU3RCUyQyUyMmluY2x1ZGVfYmFzZSUyMiUzQWZhbHNlJTJDJTIyY3JvcF9tYXJrc19ibGVlZCUyMiUzQWZhbHNlJTJDJTIyb3JpZW50YXRpb24lMjIlM0ElMjJwb3J0cmFpdCUyMiUyQyUyMmxhYmVsJTIyJTNBJTIyU3RhcnQlMjBzdGFnZSUyMiU3RCU3RA==","JTdCJTIyZGVmYXVsdCUyMiUzQSUyMiUyMiUyQyUyMmF0dHJzJTIyJTNBJTVCJTVEJTJDJTIydmFyaWF0aW9ucyUyMiUzQSU3QiU3RCU3RA==","JTdCJTdE","{}",0,1,"","2022-02-11 22:38:44","2022-03-17 23:26:52",NULL
16,"1.063' Circle",0,0,"thumbnails/lumise-media-01_round-curled-sticker-PacerPlaceholder-13.png","https://design.pacerlabels.com/data/thumbnails/YSAsea9cRpdz.png","","Available in White BOPP - Clear BOPP - Silver Metallic BOPP - Holographic Metallic BOPP and Kraft.",0,"JTdCJTIyVHlONUJiSHVRcSUyMiUzQSU3QiUyMmVkaXRfem9uZSUyMiUzQSU3QiUyMmhlaWdodCUyMiUzQTM4MSUyQyUyMndpZHRoJTIyJTNBMzgxJTJDJTIybGVmdCUyMiUzQTAuNSUyQyUyMnRvcCUyMiUzQTAuNSUyQyUyMnJhZGl1cyUyMiUzQSUyMjEwMCUyMiU3RCUyQyUyMnVybCUyMiUzQSUyMnByb2R1Y3RzJTJGbDBuMHA5Y3Muc3ZnJTIyJTJDJTIyc291cmNlJTIyJTNBJTIydXBsb2FkcyUyMiUyQyUyMm92ZXJsYXklMjIlM0FmYWxzZSUyQyUyMnByb2R1Y3Rfd2lkdGglMjIlM0E0MDAlMkMlMjJwcm9kdWN0X2hlaWdodCUyMiUzQTQwMCUyQyUyMnRlbXBsYXRlJTIyJTNBJTdCJTdEJTJDJTIyc2l6ZSUyMiUzQSU3QiUyMndpZHRoJTIyJTNBJTIyMS4xODc1JTIyJTJDJTIyaGVpZ2h0JTIyJTNBJTIyMS4xODc1JTIyJTJDJTIyY29uc3RyYWluJTIyJTNBdHJ1ZSUyQyUyMnVuaXQlMjIlM0ElMjJpbmNoJTIyJTdEJTJDJTIyaW5jbHVkZV9iYXNlJTIyJTNBZmFsc2UlMkMlMjJjcm9wX21hcmtzX2JsZWVkJTIyJTNBZmFsc2UlMkMlMjJvcmllbnRhdGlvbiUyMiUzQSUyMnBvcnRyYWl0JTIyJTJDJTIybGFiZWwlMjIlM0ElMjJTdGFydCUyMHN0YWdlJTIyJTdEJTdE","JTdCJTIyZGVmYXVsdCUyMiUzQSUyMiUyMiUyQyUyMmF0dHJzJTIyJTNBJTVCJTVEJTJDJTIydmFyaWF0aW9ucyUyMiUzQSU3QiU3RCU3RA==","JTdCJTdE","{}",0,1,"","2022-02-11 23:09:44","2022-03-17 23:27:00",NULL

Here the row of the first item (not including the title row) would be 1.0 and then the second one would be 1.063 (there are alot more).

In the second file (templates_view.csv) I have the following:

"id","width","height","attribute_item_id","created_at","updated_at","design_id"
2584,1.50,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2585,1.50,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2586,2.00,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2587,2.00,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2588,2.50,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2589,2.50,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2590,3.00,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL
2591,3.00,0.50,10,"2022-01-27 17:52:22","2022-01-27 17:52:22",NULL 

I need to check if 1.0 is in any of the rows in the entire file (templates_view.csv). My code is: import csv

with open('design_products_view.csv') as design_file, open('templates_view.csv') as templates:
    designs = csv.reader(design_file, delimiter=',')
    template_files = csv.reader(templates, delimiter=',')
    containsProduct = "Circle"

    for row in designs:
        if containsProduct in row[1]:
            rowValue = row[1].split("'")[0]
            for row in template_files:
                if row[1] != "width":
                    print(rowValue)
                    if rowValue in row[1] and row[3] == "8":
                        print(row[1])

The code executes but I only get through the first row of 'design_products_view.csv'. print(rowValue) is always 1.0. I would expect that for row in designs: would execute until completed but that is not the case. After the initial item it stops. Any idea what I'm doing wrong?

CodePudding user response:

After you loop through the inner loop one time, the file iterator is exhausted because you read the entire file. You need to either

  1. reopen the file for the inner loop every time before you do the inner loop:
with open('design_products_view.csv') as design_file:
    designs = csv.reader(design_file, delimiter=',')
    containsProduct = "Circle"

    for row in designs:
        if containsProduct in row[1]:
            rowValue = row[1].split("'")[0]
            with open('templates_view.csv') as templates: # <---- this line!
                template_files = csv.reader(templates, delimiter=',')
                for row in template_files:
                    # do whatever
  1. seek to the start of the file before you start the inner loop so the file iterator can re-read the file:
with open('design_products_view.csv') as design_file, open('templates_view.csv') as templates:
    designs = csv.reader(design_file, delimiter=',')
    template_files = csv.reader(templates, delimiter=',')
    containsProduct = "Circle"

    for row in designs:
        if containsProduct in row[1]:
            rowValue = row[1].split("'")[0]
            templates.seek(0)                # <---- this line!
            for row in template_files:
                # do whatever
  1. Read the csv file into a list and use the list:
with open('design_products_view.csv') as design_file, open('templates_view.csv') as templates:
    designs = list(csv.reader(design_file, delimiter=','))        # <---- this line!
    template_files = list(csv.reader(templates, delimiter=','))   # <---- this line!
    containsProduct = "Circle"

    for row in designs:
        if containsProduct in row[1]:
            rowValue = row[1].split("'")[0]
            for row in template_files:
                # do whatever

If you have enough memory, I'd recommend the third option because that reduces file IO which is slow.

  • Related