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:
{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
CodePudding user response:
You can't define the string outside of the loop at expect it to get filled in when printing it inside. Instead you'll have to build the string inside the loop:
systems = ["prd1", "prd2", "frz1", "frz2", "dev"]
qgsystems = ["Prd1_v2", "Prd2_v2", "Frz1_v2", "Frz2_v2", "Devl_v2"]
for x, y in zip(systems, qgsystems):
print(f"{{fn blah({x}_1_output.csv)}}select {y};")
Output:
{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;
CodePudding user response:
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))