Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsupported format character?

I am trying to print a line with a with a float formatter to 2 decimal points like this:

    print "You have a discount of 20% and the final cost of the item is'%.2f' dollars." % price

But when I do I get this error:

ValueError: unsupported format character 'a' (0x61) at index 27

What does this mean and how can I prevent it from happening?

like image 872
UsuallyExplosive Avatar asked Jun 30 '15 04:06

UsuallyExplosive


People also ask

What does it mean when a character is unsupported?

Sorry, something went wrong. Generally an unsupported format character means that the input that you are putting into (some function) needs to be read as a different encoding, typically the default is ascii and you might try utf-8. Sorry, something went wrong.

Why is my file format unsupported?

Another cause for unsupported file format is the storage issue. There are times when the memory is full, and this might cause the files not to be loaded (although this is a rare case). Sometimes having the file in the storage itself causes the file to be broken, resulting in the unsupported format. 3. Sudden shutdown of device

Why is my CSV file not converting to UTF 8?

The problem is with % characters in your csv. 2) From the ENCODING menu, select CONVERT TO UTF 8. I've had this problem several times before and it is due to special characters which are supported via ISO encoding but not supported by UTF8 encoding (which Odoo supports).

What is the character limit for a file in cloud file service?

Egnyte Cloud File Service. The limit is 245 bytes (or characters) per path component (e.g., a file or folder name) and 5000 bytes (or characters) for the entire path (including the filename). Microsoft Office imposes a 215 character limit on Office files.


2 Answers

The issue is your 20%, Python is reading ...20% and... as "% a" % price and it doesn't recognize %a as a format.

You can use 20%% as @Anand points out, or you can use string .format():

>>> price = 29.99
>>> print "You have a discount of 20% and the final cost of the item is {:.2f} dollars.".format(price)
You have a discount of 20% and the final cost of the item is 29.99 dollars.

Here the :.2f gives you 2 decimal places as with %.2f.

like image 82
Scott Avatar answered Sep 19 '22 00:09

Scott


I think the issue is with the single % sign after 20 , python maybe thinking it is a format specifier.

Try this -

print "You have a discount of 20%% and the final cost of the item is'%.2f' dollars." % price
like image 28
Anand S Kumar Avatar answered Sep 22 '22 00:09

Anand S Kumar