Home > Blockchain >  AttributeError: 'NoneType' object has no attribute 'format' with Python Format f
AttributeError: 'NoneType' object has no attribute 'format' with Python Format f

Time:06-07

I am trying to use the Python format function with the following but I keep on getting the error:

AttributeError: 'NoneType' object has no attribute 'format'

df_data.to_csv(r"/dbfs/mnt/pdf-recognized/FinalInvestmentReport/{}.txt", index=False).format("test")

Any thoughts on why I'm getting the error?

CodePudding user response:

Try this.

df_data.to_csv(r"/dbfs/mnt/pdf-recognized/FinalInvestmentReport/{}.txt".format("test"), index=False)

Error

df_data.to_csv(...) # Returns None And You try to implement format on this like

df_data.to_csv(...).format('test')

CodePudding user response:

Let's take a look at your code together and you will be able to spot the problem easily.

df_data.to_csv(r"/dbfs/mnt/pdf-recognized/FinalInvestmentReport/{}.txt", index=False).format("test")

The above code means that your code is trying to transform df_data - a dataframe to CSV, while the to_csv takes two arguments.

The first argument is the path, you are trying to format the path dynamically, but the format() method should directly follow the string that you try to format, otherwise you are telling the .format method to operate on the return value of to_csv(), which is None, now we can see the problem.

You could refer to the documentation of to_csv here,

Then, the correct way of formatting should be

"/dbfs/mnt/pdf-recognized/FinalInvestmentReport/{}.txt".format("test")

or even simpler using Python 3.6 f-string syntax

f"/dbfs/mnt/pdf-recognized/FinalInvestmentReport/{your_variable}.txt"
  • Related