When dealing with Japanese in Python, there are times when you want to check the contents of a dictionary or list. However, if Japanese is included, it cannot be output as it is. There is a convenient and easy method called prettyprint, but I will introduce it because there is an even easier method. For prettyprint, the following article will be helpful. Pretty print memo for Japanese (also python2.7 windows7)
print str(Dictionary name or list name).decode("string-escape")
Just write it like this. It's easy!
As an example, create the following list and dictionary.
>>> # -*- coding: utf8 -*-
>>> list = ['tomorrow','In the weather','What']
>>> dic = {'date':'The 10th','time':'10 o'clock'}
>>> print list
['\xe6\x98\x8e\xe6\x97\xa5','\xe5\xa4\xa9\xe6\xb0\x97\xe3\x81\xab', '\xe3\x81\xaa\xe3\x81\x82\xe3\x82\x8c']
>>> print dic
{'\xe6\x97\xa5\xe3\x81\xab\xe3\x81\xa1': '10\xe6\x97\xa5', '\xe6\x99\x82\xe9\x96\x93': '10\xe6\x99\x82'}
It looks like this, and it can't be displayed as Japanese.
>>> print str(list).decode('string-escape')
['tomorrow', 'In the weather', 'What']
>>> print str(dic).decode('string-escape')
{'date': 'The 10th', 'time': '10 o'clock'}
I was able to display it properly in Japanese!
pretty print
You can install it just by typing in the terminal as shown below.
easy_install prettyprint
Now you can use it. When actually used for a list or dictionary, it will be as follows.
>>> from prettyprint import pp
>>> pp(list)
[
"tomorrow",
"In the weather",
"What"
]
>>> pp(dic)
{
"date": "The 10th",
"time": "10 o'clock"
}
It is output in a form that is easy to see!
I introduced a super-easy way to display the contents of a Japanese dictionary or list in Python and prettyprint
.
print str (dictionary or list name) .decode ("string-escape ")
and prettyprint
should be used properly depending on the situation.
prettyprint
does line breaks, so each element is easy to see. If it's a short list, or if you just want to see the beginning of a dictionary or list to make sure it's in, then print str (dictionary or list name) .decode ("string-escape")
It may be easier to see.
-Japanese prettyprint memo (also python2.7 windows7) http://qiita.com/zaoriku0/items/f4edbb5e044648994a0d -I want to pretty print a list and dictionary including Japanese in Python http://sucrose.hatenablog.com/entry/20111112/p1
Recommended Posts