简短的回答,如果你想保持这种格式的输出,你必须自己实现它:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
my_list = ['éléphant', 'Hello World']
def print_list (l):
print ("[" + ", ".join(["'%s'" % str(x) for x in l]) + "]")
print_list (my_list)
这会产生预期的
['éléphant', 'Hello World']
但是,请注意,它会将所有元素放在引号内(例如偶数),因此如果您希望列表中包含字符串以外的任何内容,则可能需要更复杂的实现。
更长的答案
问题是 Python 在打印之前运行 str(my_list)。反过来,它会在列表的每个元素上运行 repr()。
现在,字符串上的repr() 返回字符串的纯 ASCII 表示。也就是说,您看到的那些 '\xc3' 是一个实际的反斜杠、一个实际的 'c' 和一个实际的 '3' 字符。
您无法解决这个问题,因为问题在于list.__str__ () 的实现。
下面是一个示例程序来证明这一点。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vi: ai sts=4 sw=4 et
import pprint
my_list = ['éléphant', 'Hello World']
# under the hood, python first runs str(my_list), before printing it
my_list_as_string = str(my_list)
# str() on a list runs repr() on each of the elements.
# However, it seems that __repr__ on a string transforms it to an
# ASCII-only representation
print ('str(my_list) = %s' % str(my_list))
for c in my_list_as_string:
print c
print ('len(str(my_list)) = %s' % len(str(my_list)))
print ("\n")
# Which we can confirm here, where we can see that it it also adds the quotes:
print ('repr("é") == %s' % repr("é"))
for c in repr("é"):
print c
print ('len(repr("é")) == %s' % len(repr("é")))
print ("\n")
# Even pprint fails
print ("pprint gives the same results")
pprint.pprint(my_list)
# It's useless to try to encode it, since all data is ASCII
print "Trying to encode"
print (my_list_as_string.encode ("utf8"))
生成这个:
str(my_list) = ['\xc3\xa9l\xc3\xa9phant', 'Hello World']
[
'
\
x
c
3
\
x
a
9
l
\
x
c
3
\
x
a
9
p
h
a
n
t
'
,
'
H
e
l
l
o
W
o
r
l
d
'
]
len(str(my_list)) = 41
repr("é") == '\xc3\xa9'
'
\
x
c
3
\
x
a
9
'
len(repr("é")) == 10
pprint gives the same results
['\xc3\xa9l\xc3\xa9phant', 'Hello World']
Trying to encode
['\xc3\xa9l\xc3\xa9phant', 'Hello World']