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

Can I add a variable into warnings message?

I have an application that will open many files and will convert these files into a pandas data frame. I want to ensure that all pandas’ data frames have the same structure so I want to create a warning if the number of the column is not equal that 10. I also want to know which file was the one that arise the warning.

How can I introduce the file_name into the warning message?

def load_data(self):

    some code

    file_name = file_name
    if df.shape[1] !=10:
        warning.warns("File_name is wrong"
    return pandas_df2

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 :

you can use format strings as below

wrld = 'world'
A = 2
B = 5
print(f'Hello {wrld} {A + B} times')

output :

Hello world 7 times

pretty much anything in curly braces is python code

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