简体   繁体   English

如何将unicode关键字传递给** kwargs

[英]How to pass unicode keywords to **kwargs

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 回溯(最近一次调用最后一次):TypeError中的文件“”,第1行:m()关键字必须是字符串

Am I doing something wrong or I should I fix it? 我做错了什么或者我应该修理它吗?

升级到Python 2.6.5或更高版本。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM