最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python中如何让Enum的字符串输出字段的值而不带类型的前缀

Python crifan 8305浏览 0评论

折腾:

【已解决】Flask-restful中想要使用marshal格式化enum变量

期间,对于一个Python中的枚举变量:

class ItemType(enum.Enum):
    Unknown = "Unknown" # not set yet
    Small = "Small" # like file
    Medium = "Medium" # use bag
    Large = "Large" # use box
    VeryLarge = "VeryLarge" # use car

希望对应的变量:

class EnumItem(fields.Raw):
    def format(self, value):
        gLog.debug("value=%s", value)
        return "%s" % (value)
‘itemType’ : EnumItem(attribute="itemType"),

输出的是:

value=Small

结果是:

value=ItemType.Small

python enum to string

Python Enum class (with tostring fromstring) – Stack Overflow

Enum converter in python – Stack Overflow

django – How to get all values from python enum class? – Stack Overflow

加上str(unicode)的强制转换?

结果:

class EnumItem(fields.Raw):
    def format(self, value):
        gLog.debug("value=%s", value)
        gLog.debug("str(value)=%s", str(value))
        gLog.debug("unicode(value)=%s", unicode(value))
        valueStr = "%s" % (value)
        gLog.debug("valueStr=%s", valueStr)
        return valueStr

输出:

<div–<——————————————————————————
DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:40]:
value=ItemType.Small

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:41]:
str(value)=ItemType.Small

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:42]:
unicode(value)=ItemType.Small

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:44]:
valueStr=ItemType.Small

python string enum output field value

How can I represent an ‘Enum’ in Python? – Stack Overflow

python string enum format

python enum string  format

enum in Python | Python Conquers The Universe

8.13. enum — Support for enumerations — Python 3.5.2 documentation

>>> print(Color.red.name)
red
>>> member = Color.red
>>> member.name
‘red’
>>> member.value
1

看来是可以:

通过枚举值的name和value,分别获得枚举值名字和对应的值

然后代码:

class ItemType(enum.Enum):
    Unknown = "Unknown" # not set yet
    Small = "Small" # like file
    Medium = "Medium" # use bag
    Large = "Large" # use box
    VeryLarge = "VeryLarge" # use car
class EnumItem(fields.Raw):
    def format(self, someEnum):
        gLog.debug("someEnum=%s", someEnum)
        gLog.debug("str(someEnum)=%s", str(someEnum))
        gLog.debug("unicode(someEnum)=%s", unicode(someEnum))
        someEnumStr = "%s" % (someEnum)
        gLog.debug("someEnumStr=%s", someEnumStr)
        someEnumName = someEnum.name
        someEnumValue = someEnum.value
        gLog.debug("someEnumName=%s, someEnumValue=%s", someEnumName, someEnumValue)
        return someEnumValue

就可以输出需要的结果了:

<div–<——————————————————————————
DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:40]:
someEnum=ItemType.Small

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:41]:
str(someEnum)=ItemType.Small

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:42]:
unicode(someEnum)=ItemType.Small

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:44]:
someEnumStr=ItemType.Small

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in Task [/root/RunningFast/staging/runningfast/resources/Task.py:48]:
someEnumName=Small, someEnumValue=Small

【总结】

此处,Python中的enum(注意,此处虽然是Python 2.7,但是安装了python34中enum34:

pip install enum34,所以使用起来,就是Python 3.4中的enum)

想要输出对应的字符串类型的enum中的对应的某个enum变量的值,即值的字符串,可以使用:

someEnumValue = someEnum.value

即可。

另外,想要输出枚举值中某个类型的名字,可以用:

someEnumName = someEnum.name

转载请注明:在路上 » 【已解决】Python中如何让Enum的字符串输出字段的值而不带类型的前缀

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.202 seconds, using 22.08MB memory