Python Forum
How do I add another loop to my nested loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I add another loop to my nested loop
#1
I have a working Python program (nested loop).
When the file runs:

1- Starts from first CSV file, reads first row (within a given range, e.g. 2 rows), use the data to run the function.

2- After some delay, it reads the next row and runs the function.

3- When it is done with ranges of rows on the first CSV file, after another delay, it uses the next CSV file to read the rows (within the same given range), do the same thing, when done it goes to the next CSV file, and so on until the last CSV file.

Here is the program that works fine:

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:
Quote: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

** EDITED **
The code above works fine, but the problem is:
when you run the file again, it reads the same data from CSV files, (in my example the given range is 2 rows, so every time I run it, it reads row 1 and row 2).

I want the code to use other rows as well when it runs again, within the same given range of course.
So I want to add another loop to it, which each time file runs, it starts from the next set of rows that have not been used.

as an example, if the ranges of rows are set to 2, it means it should read only 2 rows of data from each CSV file that are in
path_id_map = []
.

1- file runs (first time), reads row 1, uses the data, some delay then read row 2 uses the data, some delay, goes to next CSV file, the same process until the last CSV file.

2- file runs for the second time, it should start to use from row 3, uses the data, some delay then it should read row 4, uses the data, some delay goes to the next CSV file, the same process again until the last CSV file.
the functionality of the code remains the same.
The whole process is to read data from CSV files and run the function, no writing to CSV files.
Appreciate your help to make this work.
Reply


Messages In This Thread
How do I add another loop to my nested loop - by greenpine - Dec-24-2020, 11:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Loop through directories and files one level down? Winfried 3 237 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  Loop through all files in a directory? Winfried 10 482 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  for loop not executing Abendrot47 2 253 Apr-09-2024, 07:14 PM
Last Post: deanhystad
  Re Try loop for "net use..." failures tester_V 10 671 Mar-02-2024, 08:15 AM
Last Post: tester_V
  File loop curiously skipping files - FIXED mbk34 10 884 Feb-10-2024, 07:08 AM
Last Post: buran
  Optimise multiply for loop in Python KenBCN 4 504 Feb-06-2024, 06:48 PM
Last Post: Gribouillis
  Basic binary search algorithm - using a while loop Drone4four 1 404 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  loop through csv format from weburl in python maddyad82 3 445 Jan-17-2024, 10:08 PM
Last Post: deanhystad
  Variable definitions inside loop / could be better? gugarciap 2 475 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 654 Dec-28-2023, 05:14 PM
Last Post: trix

Forum Jump:

User Panel Messages

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