1

I have a python question and I can't figure out why this is not firing. Below is the code:

import re 
sfname='C:\python_Script\Testmain.txt' 
i=0 
Var1='AS' 
Var2=''
Var3=''

with open("C:\python_Script\Testmain.txt") as file_in:
    lines = []
    for line in file_in:
        lines.append(line)
        if line.startswith('create view [dbo].'):
            print (lines[i])
        if not line.startswith('create view [dbo].'):
            lines[i]=lines[i].replace("_"," ")
            lines[i]= re.sub(r"\bid","key",lines[i])
            Var2=lines[i]
            Var2=re.sub(r"\bkey","Dimension",Var2)
            if not line.endswith("Dimension"):
                Var2='Fact Table'
            Var3=lines[i] +"    "+Var1+"    "+Var2

            print(Var3)
        i+=1

the text file I'm reading from is relatively simple it has these attributes in it:

create view [dbo].Ignore_table
communication_channel_id
user_id
bounce_count
assignment_override_id
create view [dbo].test1
id
test1

basically I want to ignore anything with 'create view[dbo].' and create a print statement which replaces the word 'ID' with 'Key' adds an AS and then does 1 of 2 things: if) Var2 ends with 'Key' replace with the word 'Dimension'. if) var2 does not end with 'Key' then rename the whole thing 'Fact table'

but for some reason this second if the loop will fire even if it does not end with 'dimension' and it will always be 'Fact Table' Why?

2
  • 3
    Read this article for tips on debugging your code. Commented Feb 19, 2020 at 17:41
  • 2
    if not line.startswith('create view [dbo].'): could just be else: Commented Feb 19, 2020 at 17:46

3 Answers 3

1

there is some couple of variable that you do not need, like for example your variable i and the list lines which you are using them to manipulate the value of variable line during one-time iteration inside your for loop, you can use the variable line without keeping her value in a list; also you have to care that at the end of each line is the new line character \n

regarding your issue, why Var2 value is always 'Fact Table' is mainly because you are looking in the variable line instead of looking in the variable Var2 which you are modifying

here is a version of what you are trying to achive:

sfname='C:\python_Script\Testmain.txt' 
Var1='AS' 
Var2=''
Var3=''

with open(sfname) as file_in:
    for line in file_in:
        line = line.strip()
        if line.startswith('create view [dbo].'):
            print(line)

        else:
            line = line.replace("_", " ")

            # replaces the word 'ID' with 'Key' 
            line = line.replace("id","key")
            Var2 = line

            # if) Var2 ends with 'Key' replace with the word 'Dimension'.
            if Var2.endswith("key"):
                Var2 = 'Dimension'
            else:
                Var2 = 'Fact Table'

            Var3 = line + "    " + Var1 + "    " + Var2

            print(Var3)

output:

create view [dbo].Ignore_table
communication channel key    AS    Dimension
user key    AS    Dimension
bounce count    AS    Fact Table
assignment overrkeye key    AS    Dimension
create view [dbo].test1
key    AS    Dimension
test1    AS    Fact Table
Sign up to request clarification or add additional context in comments.

1 Comment

Hello, Thank you this was nearly perfect. I did have to modify it to use re.sub because it needs to match whole words, see assignement overrkeye key, on outputs.
1

Remember each variable lives somewhere in memory. When you replace \bkey with Dimension, which variable holds the new string? And which variable do you check, to see if it ends in Dimension?

Take care to pay attention to these sort of memory concerns, asking yourself whether you're updating the variable itself, or a copy of the variable. These types of bugs will pop up all the time.

Comments

1

Thank you kederrac, his answer was on point and got me to where I needed to go. I guess I didnt need to nest my if so much. for future googlers:

import re
sfname='C:\python_Script\Testmain.txt' 
Var1='AS' 
Var2=''
Var3=''

with open(sfname) as file_in:
for line in file_in:
    line = line.strip()
    if line.startswith('create view [dbo].'):
        print(line)

    else:
        line = line.replace("_", " ")

        # replaces the word 'ID' with 'Key' 
        line = re.sub(r"\bid","key",line)
        Var2 = line

        # if) Var2 ends with 'Key' replace with the word 'Dimension'.
        if Var2.endswith("key"):
            Var2 = line.replace("key","Dimension")
        else:
            Var2 = 'Fact Table'

        Var3 = line + "    " + Var1 + "    " + Var2

        print(Var3)

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.