1

I have a sql query string

query_for_update = 
f'''
update {db_name}.{schema_name}.{self.table_name}
set {self.updated_field} = {self.updated_field}
where {self.key_field} in ({ids});
'''

But when I try to write this query to file f.write(query_for_update) I get following result:

update store_1.dbo.[my_table]
            set [Trusted Plan] = [Trusted Plan]
            where [Entry No_] in (1472371,
1472375,
1472377,
1472379,
1472373,
);

Code that creates string:

ids_string = ',\n'.join(["'" + str(item) + "'" for item in result.id])
query_for_update = mssql_table.get_update_query('dbo', mssql_db_name, ids_string).strip()
with open(mssql_server_name + '.sql', 'a') as f:
    f.write(query_for_update)

How can i remove indents for strings in this case?

1
  • You should include the code which actually writes this f-string to file. Commented Jul 8, 2022 at 6:46

3 Answers 3

2

You can use textwrap.dedent (standard library):

import textwrap

query = textwrap.dedent(f"""\
    update {db_name}.{schema_name}.{self.table_name}
    set {self.updated_field} = {self.updated_field}
    where {self.key_field} in ({ids});
""")

print(query)

This will remove all leading spaces that are common between every line. Useful for tipple quoted strings.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use the str.strip() function with a for loop to fix it.

for x in list: 

  if x.strip():

    list2.append(x)

then you can use list2 as your new usable list

Comments

0

you can use the str.stip method https://www.w3schools.com/python/ref_string_strip.asp

For indentations and breaks you need to consider that you might need to use \n. There is also a dedent method in the textwrap library that could be interesting for you. https://docs.python.org/3/library/textwrap.html

I hope this helps :)

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.