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
#2
Maybe this could help:
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

from os.path import dirname as fdir
from os.path import join as fjoin
from os.path import exists as fexist
scriptdir = fdir(__file__)
currentinfo = fjoin(scriptdir, 'currentinfo.py')
if fexist(currentinfo):
    with open(currentinfo, 'r', encoding='utf-8') as fpt:
        script = fpt.read()
    exec(script, globals())
else:
    foff = 0
    rfrom = 1
    rto = 3
for pair in path_id_map:
    with open(pair['path'], 'r') as f:
        if foff == 0:
            next(f)  # skip first header line
        else:
            f.seek(foff)
        for _ in range(rfrom, rto):
            line = next(f)
            foff = f.tell()
            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)
with open(currentinfo, 'w', encoding='utf-8') as fpt:
    fpt.writelines('foff =', foff)
    fpt.writelines('rfrom =', rto)
    fpt.writelines('fto =', rto + 2)
Reply
#3
Edited
thanks for the reply and code,
I made an empty "currentinfo.py" file in the same folder that codes are.
I tired the code I get an error:
 line 41, in <module>
    if foff == 0:
NameError: name 'foff' is not defined
but is see on upper line there is:
foff = 0
Reply
#4
Well, foff is only declared in the else branch. What happens if execution goes into the if branch?
Reply
#5
I have question about business logic. Why not read all the lines at once? Or additional lines will be written during delay to files?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
Thanks for your suggestions,
perfringo, I am not sure what do you mean, as I am new to Python, and as ndc85430 suggested, I am not sure about that either, as you mentioned variable foff is only defined in else
perhaps if I define the foff, then I get errors to define the rto and rfrom variables.

If anyone can help to make it work, I appreciate it.
Reply
#7
anyone can help make this working, please?
Reply
#8
(Dec-27-2020, 07:36 PM)greenpine Wrote: perfringo, I am not sure what do you mean, as I am new to Python,

I don't understand why you need to read in three runs:

Quote:I want to add another loop to it, that each time it runs, it goes to next set of rows.

first time run, use: row 1,2
second time run, use: row 3,4
third time run, use: row 5,6

Why not read in one run all 6 rows in a way that you can access specific rows as needed and do something with them?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
thanks for the reply,
sorry, if it is confusing.

3 runs are just an example, maybe I should not put it that way.
That is to explain what the file should read when it is time for the next run.

the file is going to be scheduled to run once a day.
so on day one, it should only read and use rows 1 and 2, (of all CSV files), assuming the given range is only 2 rows.
the next day it should read and use rows 3 and 4 ( of all CSV files), and so on for the next run to come, that is what I mean.

there could be hundreds of rows in CSV files, so on each run new data should be read and used.

thanks.
Reply
#10
Yes, this is confusing. You haven't answered my question - will additional data written to files between runs? This can be the reason not read all rows at once.

It's hard to advise if it is not clear what the objective is and what are the limiting terms and conditions. Unfortunately there are no clues for me in the code - i have no idea what it suppose to do and why this way.

As the sidenote - if I see exec (or eval) I tend to think: 'somebody is trying to shoot his foot'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Loop through all files in a directory? Winfried 10 316 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  for loop not executing Abendrot47 2 209 Apr-09-2024, 07:14 PM
Last Post: deanhystad
  Re Try loop for "net use..." failures tester_V 10 606 Mar-02-2024, 08:15 AM
Last Post: tester_V
  File loop curiously skipping files - FIXED mbk34 10 801 Feb-10-2024, 07:08 AM
Last Post: buran
  Optimise multiply for loop in Python KenBCN 4 488 Feb-06-2024, 06:48 PM
Last Post: Gribouillis
  Basic binary search algorithm - using a while loop Drone4four 1 368 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  loop through csv format from weburl in python maddyad82 3 408 Jan-17-2024, 10:08 PM
Last Post: deanhystad
  Variable definitions inside loop / could be better? gugarciap 2 442 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 620 Dec-28-2023, 05:14 PM
Last Post: trix
  Need an alternative to brute force optimization loop jmbonni 5 1,180 Dec-07-2023, 12:28 PM
Last Post: RockBlok

Forum Jump:

User Panel Messages

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