[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python

Introduction

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)

Super easy way

print str(Dictionary name or list name).decode("string-escape")

Just write it like this. It's easy!

Comparison

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'}

Output normally

>>> 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 (dictionary or list name) .decode ("string-escape")

>>> 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!

Summary

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.

Reference URL

-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

[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
How to display bytes in the same way in Java and Python
How to get the number of digits in Python
[python] Summary of how to retrieve lists and dictionary elements
Comparison of how to use higher-order functions in Python 2 and 3
How to handle Japanese in Python
How to check if the contents of the dictionary are the same in Python by hash value
How to determine the existence of a selenium element in Python
How to know the internal structure of an object in Python
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
[EC2] How to install chrome and the contents of each command
How to check the memory size of a variable in Python
[Python] How to get the first and last days of the month
How to check the memory size of a dictionary in Python
[TensorFlow 2] How to check the contents of Tensor in graph mode
How to sort 2D arrays, dictionaries and lists of proprietary classes
How to display multiplication table in python
How to display python Japanese with lolipop
How to display Hello world in python
How to use is and == in Python
How to write the correct shebang in Perl, Python and Ruby scripts
How to get the date and time difference in seconds with python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to compare if the contents of the objects in scipy.sparse.csr_matrix are the same
How to format a list of dictionaries (or instances) well in Python
How to copy and paste the contents of a sheet in Google Spreadsheet in JSON format (using Google Colab)
How to use the C library in Python
[Python] How to change the date format (display format)
Summary of how to import files in Python 3
Summary of how to use MNIST in Python
How to get the files in the [Python] folder
I want to display the progress in Python!
How to plot autocorrelation and partial autocorrelation in python
How to pass the execution result of a shell command in a list in Python
How to count the number of elements in Django and output to a template
How to find the coefficient of the trendline that passes through the vertices in Python
About the handling of ZIP files including Japanese files when upgrading from Python2 to Python3
How to get a list of files in the same directory with python
(Diary 1) How to create, reference, and register data in the SQL database of Microsoft Azure service with python
Easy way to check the source of Python modules
How to retrieve the nth largest value in Python
How to get the variable name itself in python
How to know the current directory in Python in Blender
How to display multiple images of galaxies in tiles
Not being aware of the contents of the data in python
[Python] How to sort dict in list and instance in list
[Python] Summary of how to specify the color of the figure
How to use the model learned in Lobe in Python
Python # How to check type and type for super beginners
Save lists, dictionaries and tuples to external files python
[Python] How to output the list values in order
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Python OpenCV tried to display the image in text.
How to display the modification date of a file in C language up to nanoseconds
How to store Python function in Value of dictionary (dict) and call function according to Key
How to identify the element with the smallest number of characters in a Python list?
[Note] How to write QR code and description in the same image with python
How to check in Python if one of the elements of a list is in another list
How to count the number of occurrences of each element in the list in Python with weight
[Implementation explanation] How to use the Japanese version of BERT in Google Colaboratory (PyTorch)
It is easy to execute SQL with Python and output the result in Excel