我有一个带有编码问题的dict列表mylist
:
>>print mylist
[{u'param1': u'foo',u'param2': u'Special char \xe9'}, ...]
>>for item in mylist:
>> print item
{u'param1': u'foo',u'param2': u'Special char \xe9'}
...
>>for item in input:
>> for i in item:
>> print 'Key: %s Value: %s' % (i, item[i])
Key: param1
Value: foo
Key: param2
Value: Special char é
(\xe9
是é
)
如何直接修改编码:
>>print mylist
[{param1: 'foo',param2: 'Special char é'}, ...]
答案 0 :(得分:1)
实际上它已经以正确的方式编码。但是,如果您print
是一个字典,它会在键和值上调用repr(..)
,然后使用转义编码。但是,如果您print(..)
具有特定值,则会根据需要显示该值,因为使用了str(..)
。 \xe9
仅供程序员使用,以便您可以轻松复制输出并将其提供给程序:
$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> mylist = [{u'param1': u'foo',u'param2': u'Special char \xe9'}]
>>> print(mylist[0]['param2'])
Special char é