[PYTHON] Output unicode string list

If it is a list, it will try to use the str function, so Print on the element or assign in format to the unicode type to print. It's troublesome when the number of elements increases.

Referenced site: Knowing Python's UnicodeEncodeError

# -*- coding:utf-8 -*-
#Check the default value that appears on the above site.
import sys, codecs
#For some reason, the one on the next line may not pass. Is it a version issue?#sys.stdout = codecs.EncodedFile(sys.stdout, 'utf_8')
sys.stdout = codecs.lookup(u'utf_8')[-1](sys.stdout)
print "sys.getdefaultencoding() => ",sys.getdefaultencoding()
print "sys.stdout.encoding => ",sys.stdout.encoding

#Test from here

alist = [ u"Unicode Japanese", u"That's right"]

for st in alist :
    print st

print u"{0}{1}".format(*alist)

print map(str,alist)

The following is the output.

sys.getdefaultencoding() =>  ascii
sys.stdout.encoding =>  UTF-8
Unicode Japanese
That's right
Unicode Japanese
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    202             else:
    203                 filename = fname
--> 204             __builtin__.execfile(filename, *where)

/Users/yuuichi/test-print/test.py in <module>()
     15 print u"{0}{1}".format(*alist)
     16
---> 17 print map(str,alist)

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)

Recommended Posts

Output unicode string list
Output the line containing the specified string
Output 2017 Premium Friday list in Python
numpy> Random number list output> zip () / print%
String conversion of a list containing numbers
Join List elements without''. (Retrieve String from list without'')