[PYTHON] Creating a simple table using prettytable

Creating a table with prettyprint

Click here for details: https://pypi.python.org/pypi/PrettyTable I installed the following with pip. This is convenient when the length of each element of list is different and it is difficult to see even if you try to output it. This time code: https://github.com/KodairaTomonori/Qiita/blob/master/module/prettyprint/make_table.py

pip.


$ pip search prettytable
prettytable-extras     - An extension to the excellent prettytable Python library

I will actually make it

This time, I will explain using mecab, which analyzes morphological elements. When you put a sentence, mecab returns the morphological analysis result as a character string. This can also be easily installed with pip.

mecab_test.


$ python
Python 3.5.0
>>> import MeCab
>>> MeCab.Tagger().parse('The body is made of a sword')
'body\t noun,General,*,*,*,*,body,Body,Body\n is\t particle,Particle,*,*,*,*,Is,C,Wow\n sword\t noun,General,*,*,*,*,sword,Ken,Ken\at n\t particle,Case particles,General,*,*,*,so,De,De\n done\t verb,Independence,*,*,One step,Continuous form,Can do,Deキ,Deキ\n\t particle,Connection particle,*,*,*,*,hand,Te,Te\n\t verb,非Independence,*,*,One step,Uninflected word,Is,Il,Il\nEOS\n'
>>> print(MeCab.Tagger().parse('The body is made of a sword') )
Body noun,General,*,*,*,*,body,Body,Body
Is a particle,Particle,*,*,*,*,Is,C,Wow
Sword noun,General,*,*,*,*,sword,Ken,Ken
Particles,Case particles,General,*,*,*,so,De,De
Verb,Independence,*,*,One step,Continuous form,Can do,Deki,Deki
Particles,Connection particle,*,*,*,*,hand,Te,Te
Verb,Non-independent,*,*,One step,Uninflected word,Is,Il,Il
EOS

It will be output like this, but if you just print it, it will be difficult to see because there is nothing behind it So I'd like to use pretty table to make this easier to see.

make_prettytable.py


import MeCab
import prettytable

def make_prettytable(others, header=[]):
    if not header: header = range(len(list(others)[0]) )
    if len(header) != len(others[0]):
        print('incorrect length!')
        return 
    table = prettytable.PrettyTable(header)
    for other in others: table.add_row(other)
    return table

def make_mecab_info_table(sentence, output_info=[], \
        mecab_parser=MeCab.Tagger.parse):
    others = list(map(lambda x: x.split(','), \
        mecab_parser(sentence).replace('\t', ',').split('\n') ) )[:-2]
    return make_prettytable(others, output_info)


if __name__ == '__main__':
    mecab_output = 'Surface type,Part of speech,Part of speech細分類1,Part of speech細分類2,Part of speech細分類3,Inflected form,Utilization type,Prototype,reading,pronunciation'
    parser = MeCab.Tagger().parse
    header = mecab_output.split(',')
    print(make_mecab_info_table(input(), header, parser) )

Output example

スクリーンショット 2015-10-21 21.29.05.png

Commentary

It became easier to see like this. make_prettytable contains the contents in the first argument (ʻothers) and the information in the upper row in the second argument ( header). header assigns a number with range if it does not receive an argument. ʻOthers should include a list like[[1,2, ...], [2,3, ...] ...]. In prettytable.PrettyTable, put the upper row ( header) and make the base of the table. In the following for statement, ʻothers is turned and .add_row` is added line by line. After that, you can easily see the table just by printing.

make_mecab_info_table just handles characters normally.

It is convenient when the lengths of the elements of list are different, because it is difficult to see even if you try to output and check.

Recommended Posts

Creating a simple table using prettytable
Creating a web application using Flask ②
Learn Zundokokiyoshi using a simple RNN
Creating a simple app with flask
Creating a web application using Flask ①
Creating a learning model using MNIST
Creating a web application using Flask ③
Creating a web application using Flask ④
Creating a simple PowerPoint file with Python
Creating a data analysis application using Streamlit
[Python] Scraping a table using Beautiful Soup
A story about simple machine learning using TensorFlow
Creating an interactive application using a topic model
Sample to draw a simple clock using ebiten
[Day 9] Creating a model
Creating a Home screen
Try creating a compressed file using Python and zlib
4. Creating a structured program
Creating a graph using the plotly button and slider
Creating a scraping tool
Creating a dataset loader
Create a dictionary by searching the table using sqlalchemy
100 Language Processing Knock-84 (using pandas): Creating a word context matrix
A memo when creating a directed graph using Graphviz in Python
(For beginners) Try creating a simple web API with Django
Create a simple CRUD app using Django's generic class view
The story of creating a database using the Google Analytics API
I tried to make a simple text editor using PyQt
A simple sample of pivot_table.
Creating Spigot plugins using Eclipse
Time measurement using a clock
Create a (simple) REST server
Try creating a CRUD function
# 1 [python3] Simple calculation using variables
Pepper Tutorial (5): Using a Tablet
Using a printer with Debian 10
Create a simple textlint server
A memorandum of using eigen3
Make a simple CO2 incubator using Raspberry PI and CO2 sensor (MH-Z14A)
Create and deploy a Django (PTVS) app using Azure Table storage
Create a simple scheduled batch using Docker's Python Image and parse-crontab
Create a pandas Dataflame by searching the DB table using sqlalchemy
How to format a table using Pandas apply, pivot and swaplevel
A note I was addicted to when creating a table with SQLAlchemy
Evaluate the performance of a simple regression model using LeaveOneOut cross-validation
[Python] Analyze Splatoon 2 league match data using a correlation coefficient table
Creating a Shogi Game Record Management App Using Django 1-Environment Construction-