开发者

How to pass unicode keywords to **kwargs

开发者 https://www.devze.com 2023-02-03 01:27 出处:网络
I was exception the following to work. def foo(**kwargs): print kwargs foo(**{\'a\':\'b\'}) foo(**{u\'a\':\'b\'})

I was exception the following to work.

def foo(**kwargs):
    print kwargs
foo(**{'a':'b'})
foo(**{u'a':'b'})

Traceback (most recent call last): File "", line 1, in TypeError: m() keywords must be strings

Am I doing something wrong or I should I fi开发者_Python百科x it?


Upgrade to Python 2.6.5 or later.


Upgrading wasn't an option for me so I'm calling this on dicts as needed--

def flatten_unicode_keys(d):
    for k in d:
        if isinstance(k, unicode):
            v = d[k]
            del d[k]
            d[str(k)] = v
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号