need help to add a loop to a nested loop Python code.

007

Registered Member
Joined
Dec 21, 2007
Messages
63
Reaction score
2
Hi, I am new to Python and trying to add another loop to the code I have, but I can not come up with the solution.
Here is what the code does:
  1. It starts with the first CSV file, which reads from the first row (there is a given range to use), uses the data to run the function. After a few seconds of delay, it goes to the next row and does the same, same process to use all rows (within a given range).
  2. After a few seconds of delay, it goes to the next CSV file, does the same process as above (delay between each row), until all CSV files are used.
The problem is, when I run the file for the second time and more, it uses the same rows.

I want to add another loop to it, so when I run it again, it iterates over the next row that has not been used.

This is how it should work, assume the given range is: 2 rows.

the first-time run uses rows 1 and 2 for all CSV files.

second-time run uses rows 3 and 4 for all CSV files and so on.

Here is code that works ( one time use):
Code:
from abc.zzz  import xyz

path_id_map = [
    {'path':'file1.csv', 'id': '12345678'},
    {'path':'file2.csv', 'id': '44556677'}
    {'path':'file3.csv', 'id': '33377799'}
    {'path':'file4.csv', 'id': '66221144'}]
s_id = None

for pair in path_id_map:
    with open(pair['path'], 'r') as f:
        next(f)  # skip first header line
        for _ in range(1, 3):     
            line = next(f)
            img_url, title_1, desc_1, link_1 = map(str.strip, line.split(';'))
            zzz.func1(img_url=img_url, title_1=title_1, desc_1=desc_1,
                      link_1=link_1, B_id=B_id=pair['id'], s_id=s_id)
            time.sleep(25)

sample CSV file:

Code:
img_url,desc_1 title_1,link_1
site.com/image22.jpg;someTitle;description1;site1.com
site.com/image32.jpg;someTitle;description2;site2.com
site.com/image44.jpg;someTitle;description3;site3.com

I appreciate to your help to make it work.
 
This is probably no longer relevant, but most likely you can try to solve the problem using function closure
 
Back
Top