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

How to add space in data/String using python?

I have some data in my database. It looks like 84819010 and I wanted to make it like 8481 90 10
I use the following code and get the following error:

information_table = data.cur.execute("""
                    SELECT p.gs_com_code, p.gs_code FROM product p
                    INNER JOIN (
                    SELECT pp.p_gs_com_code From product_purchase pp
                    ) t ON t.p_gs_com_code = p.gs_com_code                           
                         """,).fetchall()

for info in information_table:
    t = info[1]
    sep = "{}{}{}{} {}{} {}{}"
    gs_code = sep.format(*t)
    print(gs_code)

Error:

    gs_code = sep.format(*t)
IndexError: Replacement index 7 out of range for positional args tuple

My information looks like 84819010 and I wanted to make it like 8481 90 10

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

>Solution :

Use f-strings:

for info in information_table:
    gs_code = f"{info[:4]} {info[4:6]} {info[6:]}"
    print(gs_code)

# Output
6804 22 00
7018 10 00
8712 00 00
6804 22 00
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