5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Pythonでのデータ埋める方法

Posted at

#実装

padding_formatter.py
def padding_formatter(item, padding, data_type):
    """ 引数で指定された桁数までデータを埋める
        埋める内容は以下の通り
        ・データ型[numeric: 数字]   先頭0埋め
        ・データ型[half: 半角]      後ろ半角スペース埋め
        ・データ型[full: 全角]      後ろ全角スペース埋め

    Args:
        item: 項目
        padding: 桁数(byte数ではない)
                例  全角で64byte項目の場合は「32」桁を指定
        data_type: データ型[numeric: 数字, half: 半角, full: 全角]
    Returns:
        固定長分埋めた項目
    """
    out_item = str(item)
    if data_type == 'numeric':
        out_item = str(item).zfill(padding)
    elif data_type == 'half':
        out_item = str(item).ljust(padding, ' ')
    elif data_type == 'full':
        out_item = str(item).ljust(padding, ' ')
    return out_item

#参考文献
https://note.nkmk.me/python-zero-padding/

5
4
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?