失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python列表输出字符串 如何在python中输出utf-8字符串列表?

python列表输出字符串 如何在python中输出utf-8字符串列表?

时间:2023-12-18 17:37:15

相关推荐

python列表输出字符串 如何在python中输出utf-8字符串列表?

Well, character encoding and decoding sometimes frustrates me a lot.

So we know u'\u4f60\u597d' is the utf-8 encoding of 你好,

>>> print hellolist

[u'\u4f60\u597d']

>>> print hellolist[0]

你好

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?

解决方案

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 .

Example of 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.

Example -

>>> h1 = [h,u'\u4f77\u587f']

>>> print u'[' + u','.join([u"'" + unicode(i) + u"'" for i in h1]) + u']'

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('','w') as f:

f.write(listprinter(l))

如果觉得《python列表输出字符串 如何在python中输出utf-8字符串列表?》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。