0

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

2
  • 1
    You are referring to variable x where the x is not defined. You may use string formatting instead of fstring. Commented Apr 22, 2022 at 22:40
  • Strings are immutable, and the variables inside an f-string are evaluated once in order to produce the string contents at the time the f-string is defined, not every time the string is referenced thereafter. If you use str.format() as MSH suggests, the format() function will give you a new string each time it's called. Commented Apr 22, 2022 at 22:49

2 Answers 2

4

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;
Sign up to request clarification or add additional context in comments.

Comments

3
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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.