问题描述
我从代码的一个层"接收到一个字典,在将其传递到另一个层"之前,在该层"上执行了一些计算/修改.原始字典的键 &字符串"值是 unicode
,但它们被传递到的层只接受 str
.
I'm receiving a dict from one "layer" of code upon which some calculations/modifications are performed before passing it onto another "layer". The original dict's keys & "string" values are unicode
, but the layer they're being passed onto only accepts str
.
这将经常被调用,所以我想知道什么是最快的转换方式:
This is going to be called often, so I'd like to know what would be the fastest way to convert something like:
{ u'spam': u'eggs', u'foo': True, u'bar': { u'baz': 97 } }
...到:
{ 'spam': 'eggs', 'foo': True, 'bar': { 'baz': 97 } }
...记住非字符串"值需要保持其原始类型.
...bearing in mind the non-"string" values need to stay as their original type.
有什么想法吗?
推荐答案
DATA = { u'spam': u'eggs', u'foo': frozenset([u'Gah!']), u'bar': { u'baz': 97 },
u'list': [u'list', (True, u'Maybe'), set([u'and', u'a', u'set', 1])]}
def convert(data):
if isinstance(data, basestring):
return str(data)
elif isinstance(data, collections.Mapping):
return dict(map(convert, data.iteritems()))
elif isinstance(data, collections.Iterable):
return type(data)(map(convert, data))
else:
return data
print DATA
print convert(DATA)
# Prints:
# {u'list': [u'list', (True, u'Maybe'), set([u'and', u'a', u'set', 1])], u'foo': frozenset([u'Gah!']), u'bar': {u'baz': 97}, u'spam': u'eggs'}
# {'bar': {'baz': 97}, 'foo': frozenset(['Gah!']), 'list': ['list', (True, 'Maybe'), set(['and', 'a', 'set', 1])], 'spam': 'eggs'}
假设:
- 您已经导入了 collections 模块并且可以使用它提供的抽象基类
- 您很乐意使用默认编码进行转换(如果您需要明确的编码,请使用
data.encode('utf-8')
而不是str(data)
编码).
- You've imported the collections module and can make use of the abstract base classes it provides
- You're happy to convert using the default encoding (use
data.encode('utf-8')
rather thanstr(data)
if you need an explicit encoding).
如果您需要支持其他容器类型,希望很明显如何遵循该模式并为它们添加案例.
If you need to support other container types, hopefully it's obvious how to follow the pattern and add cases for them.
这篇关于转换字典键的最快方法从`unicode`到`str`的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!