Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python help needed on a loop with multiple variables

Basically I want to iterate 5 times through a list of two variables and populate the output iteratively. I’m probably not describing that well enough, so I’ll show what I’m trying to get. I will have a multi-line query in the query2 variable, but I simplified it a bit for the question here.

Here is the code I’m working on:

systems = ["prd1", "prd2", "frz1", "frz2", "dev"]
qgsystems = ["Prd1_v2", "Prd2_v2", "Frz1_v2", "Frz2_v2", "Devl_v2"]
query2 = f"""{{fn blah({x}_1_output.csv)}}select {y};"""
for x, y in zip(systems, qgsystems):
    print(query2)

The output I’m trying to get would be this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{fn blah(prd1_1_output.csv)}select Prd1_v2;
{fn blah(prd2_1_output.csv)}select Prd2_v2;
{fn blah(frz1_1_output.csv)}select Frz1_v2;
{fn blah(frz2_1_output.csv)}select Frz2_v2;
{fn blah(dev_1_output.csv)}select Devl_v2;

But instead I’m getting this:

{fn blah(dev_1_output.csv)}select Devl_v2;
{fn blah(dev_1_output.csv)}select Devl_v2;
{fn blah(dev_1_output.csv)}select Devl_v2;
{fn blah(dev_1_output.csv)}select Devl_v2;
{fn blah(dev_1_output.csv)}select Devl_v2;

What am I doing wrong?

When I start a fresh session, I get closer to what’s wrong. But I’m kinda stuck still.

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-3ea2eb0d6f53> in <module>
      1 systems = ["prd1", "prd2", "frz1", "frz2", "dev"]
      2 qgsystems = ["Prd1_v2", "Prd2_v2", "Frz1_v2", "Frz2_v2", "Devl_v2"]
----> 3 query2 = f"""{{fn blah({x}_1_output.csv)}}select {y};"""
      4 for x, y in zip(systems, qgsystems):
      5     print(query2)

NameError: name 'x' is not defined

>Solution :

systems = ["prd1", "prd2", "frz1", "frz2", "dev"]
qgsystems = ["Prd1_v2", "Prd2_v2", "Frz1_v2", "Frz2_v2", "Devl_v2"]
query2 = "{{fn blah({x}_1_output.csv)}}select {y};"
for x, y in zip(systems, qgsystems):
    print(query2.format(x=x, y=y))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading