如何输出utf-8字符串列表,就像在python中一样? [英] How to output a utf-8 string list as it is in python?
问题描述
嗯,字符编码和解码有时让我非常沮丧.
Well, character encoding and decoding sometimes frustrates me a lot.
所以我们知道u'\u4f60\u597d'
是你好
的utf-8
编码
>>> print hellolist
[u'\u4f60\u597d']
>>> print hellolist[0]
你好
现在我真正想从输出或写入文件中得到的是[u'你好']
,但是一直都是[u'\u4f60\u597d']
,那么你怎么做呢?
Now what I really want to get from the output or write to a file is [u'你好']
, but it's [u'\u4f60\u597d']
all the time, so how do you do it?
推荐答案
打印(或写入文件)列表时,列表内部调用列表的str()
方法,但是列表内部对其列表调用repr()
元素. repr()
返回您所看到的难看的unicode表示形式.
When you print (or write to a file) a list it internally calls the str()
method of the list , but list internally calls repr()
on its elements. repr()
returns the ugly unicode representation that you are seeing .
repr的示例-
>>> h = u'\u4f60\u597d'
>>> print h
\u4f60\u597d
>>> print repr(h)
u'\u4f60\u597d'
您需要手动获取列表中的元素并进行打印,以使其正确打印.
You would need to manually take the elements of the list and print them for them to print correctly.
示例-
>>> h1 = [h,u'\u4f77\u587f']
>>> print u'[' + u','.join([u"'" + unicode(i) + u"'" for i in h1]) + u']'
对于包含可能具有unicode字符的子列表的列表,您将需要一个递归函数,例如-
For lists containing sublists that may have unicode characters, you would need a recursive function , example -
>>> h1 = [h,(u'\u4f77\u587f',)]
>>> def listprinter(l):
... if isinstance(l, list):
... return u'[' + u','.join([listprinter(i) for i in l]) + u']'
... elif isinstance(l, tuple):
... return u'(' + u','.join([listprinter(i) for i in l]) + u')'
... elif isinstance(l, (str, unicode)):
... return u"'" + unicode(l) + u"'"
...
>>>
>>>
>>> print listprinter(h1)
要将它们保存到文件中,请使用相同的列表理解或递归函数.示例-
To save them to file, use the same list comprehension or recursive function. Example -
with open('<filename>','w') as f:
f.write(listprinter(l))
这篇关于如何输出utf-8字符串列表,就像在python中一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!