0

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

  • 1
    Can you use an f-string? `f"Filename {file_name} is wrong"`? – Jack Deeth Feb 09 '22 at 21:37
  • As a follow-up, does anybody know why this isn't standard practice? Many packages I use will throw errors with unhelpful messages that could include the information about the variable that caused the error to be thrown. – Pepsi-Joe Feb 09 '22 at 22:00

1 Answers1

2

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

  • IMO this is not all there it is to warnings. I am at a point where if i add a variable inside warnings.warn(variable) it will not give me the variable contents – Taavi Ansper Oct 06 '22 at 09:39