Python “unicode”类型对象的未知格式代码“f”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15924879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:23:22  来源:igfitidea点击:

Unknown format code 'f' for object of type 'unicode'

pythondjango

提问by MarkO

can someone tell me what is wrong with this code...

谁能告诉我这段代码有什么问题...

def format_money_value(num):
    return u'{0:.2f}'.format(num)

It gives me the following error:

它给了我以下错误:

Unknown format code 'f' for object of type 'unicode'

I'm running Django 1.5

我正在运行 Django 1.5

Thank you

谢谢

回答by Bakuriu

In your case numis a unicode string, which does not support the fformat modifier:

在您的情况下num是一个 unicode 字符串,它不支持f格式修饰符:

>>> '{0:.2f}'.format(u"5.0")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'f' for object of type 'unicode'

You can fix the error making the conversion to floatyourself:

您可以修复转换为float自己的错误:

>>> '{0:.2f}'.format(float(u"5.0"))
'5.00'


As pointed out by mgilson when you do '{0:.2f}'.format(num), the formatmethod of the strings calls num.__format__(".2f"). This results in an error for stror unicode, because they don't know how to handle this format specifier. Note that the meaning of fis left as an implementation for the object. For numeric types it means to convert the number to a floating point string representation, but other objects may have different conventions.

正如 mgilson 指出的那样'{0:.2f}'.format(num)format字符串的方法调用num.__format__(".2f"). 这会导致stror错误unicode,因为他们不知道如何处理这个格式说明符。请注意, 的含义f保留为对象的实现。对于数字类型,这意味着将数字转换为浮点字符串表示,但其他对象可能有不同的约定。

If you used the %formatting operator the behaviour is different, because in that case %fcalls __float__directly to obtain a floating point representation of the object. Which means that when using %-style formatting fdoeshave a specific meaning, which is to convert to a floating point string representation.

如果使用%格式化运算符,则行为会有所不同,因为在这种情况下,直接%f调用__float__以获取对象的浮点表示。这意味着在使用%-style 格式时f确实具有特定含义,即转换为浮点字符串表示形式。

回答by FallenAngel

what .format() do

.format() 做什么

str.formatmethod calls __format__()method of related type. That means

str.format方法调用 __format__()相关类型的方法。这意味着

<type>.__format__(<value>, <spec>)

above method accepts the same type argument as first value, and accepts a suitable spectype as second one. Like,

上述方法接受与第一个值相同的类型参数,并接受合适的spec类型作为第二个值。喜欢,

str.__format__('1', 's')
int.__format__(1, 'f')
float.__format__(1.00, 'f')

str.__format__accepts any type that is derived from strtype, like stror unicode. Spec value must be a valid formatter that is usable of that type. Following will raise an error

str.__format__接受从str类型派生的任何类型,如strunicode。Spec 值必须是可用于该类型的有效格式化程序。以下将引发错误

str.__format__('1', 'f')
ValueError: Unknown format code 'f' for object of type 'str'

since floating pointformatting is not a suitableformat type fot string. Likewise following will raise an error too

因为floating point格式化不是一个合适的格式类型 fot 字符串。同样,以下也会引发错误

float.__format__(1.00, 's')
ValueError: Unknown format code 's' for object of type 'float'

since float is a numerictype and can not formatted as a string. But following are all valid:

因为 float 是一种numeric类型,不能格式化为string. 但以下都是有效的:

float.__format__(1.00, 'g')
float.__format__(1.00, 'f')

similarly following will raise an exception

同样以下将引发异常

float.__format__(1.00, 'd')
ValueError: Unknown format code 'd' for object of type 'float'

since formatting a float point to a decimal value will cause precision values to be lost. But formatting an intto floatwill not cause a such thing, so it is a valid conversion:

因为将浮点数格式化为十进制值会导致精度值丢失。但是格式化一个inttofloat不会导致这样的事情,所以它是一个有效的转换:

int.__format__(1, 'f')

So .format()is limeted to specsthat is available to the related formatting type. You must parse your value as @Bakuriu defined:

所以.format()被limeted到specs这是提供给相关的格式化type。您必须按照@Bakuriu 的定义解析您的值:

'{0:.2f}'.format(float(u"5.0"))

回答by cnaak

I've faced a similar problem as the OP, when numwas a numerical answer returned by one of my libraries, whose implementation detais got forgotten:

我遇到了与 OP 类似的问题,当num我的一个库返回一个数字答案时,它的实现细节被遗忘了:

>>> num
-4132.56528214700
>>> u'{0:.4g}'.format(num)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'g' for object of type 'unicode'

I was really puzzled because numbehavedlike a float but after testing @Bakuriu 's solution, I've found out it wasn't a float:

我真的很困惑,因为num表现得像一个浮点数,但在测试 @Bakuriu 的解决方案后,我发现它不是浮点数:

>>> type(num)
<class 'sympy.core.numbers.Float'>

So @Bakuriu 's solution was right on target for my case:

所以@Bakuriu 的解决方案对我来说是正确的:

>>> u'{0:.4g}'.format(float(num))
u'-4133'

Therefore, the error can be due to types that display/calculate like but aren't really floats.

因此,错误可能是由于显示/计算的类型,但不是真正的浮点数。

回答by Timothy Jannace

The scenario where you are re-formatting a string (unicode or otherwise) as a float string is not very safe. You should first convert the string to a numeric representation and only if that succeeds should you format it as a string again. You are not in control of the data that comes into your program so you should be sure to validate it coming in.

将字符串(unicode 或其他)重新格式化为浮点字符串的情况不是很安全。您应该首先将字符串转换为数字表示,只有在成功时才应将其再次格式化为字符串。您无法控制进入程序的数据,因此您应该确保验证进入的数据。

If you choose to leave it as a string you can use this:

如果您选择将其保留为字符串,则可以使用以下命令:

return u"{:.2f}".format(num) if num.isnumeric() else u"{}".format(num)

If you have already converted the string into a numeric format, you can adjust your formatter like this:

如果您已经将字符串转换为数字格式,您可以像这样调整格式化程序:

return u"{:.2f}".format(num) if isinstance(num, NumberTypes) else u"{}".format(num)