【Python】Python ~ CSVデータ生成ツールを作ってみる ~

■ はじめに

https://dk521123.hatenablog.com/entry/2022/11/28/000000

で、JavaScriptでファイル出力するCSVデータ生成ツールを作ってみたが
もう少し、細かくカスタマイズしたいので、Pythonで書く。
こういう仕事多いので、作っておかないと時間がもったいないので。

目次

【1】簡単な指針
【2】サンプル
 例1:シンプル版
 例2:発展版

【1】簡単な指針

* 余計なライブラリは使わず、できる限り、pipせずに、標準ライブラリで賄う

【2】サンプル

例1:シンプル版

import os
import csv
import datetime


def generate_csv(file_path, body_data, header_list=[], delimiter=","):
  with open(file_path, 'w', newline="") as csv_file:
    writer = csv.writer(csv_file, delimiter=delimiter)
    if (len(header_list) != 0):
      writer.writerow(header_list)
    for row in body_data:
      writer.writerow(row)

# To initialize
output_path = './output/test'
os.makedirs(output_path, exist_ok=True)

# Case1: making manual
base_file_name = "person"
extension = ".csv"
header_list = ["id", "name", "city", "age"]
body_data = [
  ["001", "Mike", "Tokyo", 29],
  ["002", "Tom", "Osaka", 32],
]

now_datetime = datetime.datetime.now()
today = now_datetime.strftime('%Y%m%d')

generate_csv(
  f"{output_path}/{base_file_name}_{today}_001{extension}",
  body_data,
  header_list,
  "|"
)
generate_csv(
  f"{output_path}/{base_file_name}_{today}_002{extension}",
  body_data
)

output/test/person_20230419_001.csv

id|name|city|age|remarks
001|Mike|Tokyo|29
002|Tom|Osaka|32

output/test/person_20230419_002.csv

001,Mike,Tokyo,29
002,Tom,Osaka,32

例2:発展版

import os
import csv
import datetime
import random
import string

def generate_csv(file_path, body_data, header_list=[], delimiter=","):
  with open(file_path, 'w', newline="") as csv_file:
    writer = csv.writer(csv_file, delimiter=delimiter)
    if (len(header_list) != 0):
      writer.writerow(header_list)
    for row in body_data:
      writer.writerow(row)

person_name_list = ["Mike", "Tom", "Kevin", "Smith"]
city_list = ["Tokyo", "Osaka", "Fukuoka"]
date_list = ["2022-12-31", "2019-01-01", "2023-08-15", "2020-01-29"]

def generate_body(header_list, max_line=10):
  csv_body = []
  for index in range(max_line):
    row = []
    for csv_item in header_list:
      value = ""
      item = csv_item.lower()
      if item == 'id':
        id = str(index).zfill(10)
        # e.g. 44062120-d060-4c21-9cb5-1000000000
        value = f"44062120-d060-4c21-9cb5-{id}"
      elif item == 'name':
        value = get_value_from_list(index, person_name_list)
      elif item == 'city':
        value = get_value_from_list(index, city_list)
      elif item == 'age':
        value = str(int(random.uniform(15, 80)))
      elif item == 'registration_date':
        value = get_value_from_list(index, date_list)
      else:
        value = "aaa"
      row.append(value)
    csv_body.append(row)
  return csv_body

def get_value_from_list(index, target_list):
  target_index = index % (len(target_list))
  return target_list[target_index]

# To initialize
output_path = './output/test'
os.makedirs(output_path, exist_ok=True)

now_datetime = datetime.datetime.now()
today = now_datetime.strftime('%Y%m%d')

# Case2
base_file_name = "person"
extension = ".csv"
header_list = ["id", "name", "city", "age", "registration_date", "remarks"]
body_data = generate_body(header_list, 13)

generate_csv(
  f"{output_path}/{base_file_name}_{today}_003{extension}",
  body_data,
  header_list,
)

output/test/person_20230419_003.csv

id,name,city,age,registration_date,remarks
44062120-d060-4c21-9cb5-0000000000,Mike,Tokyo,47,2022-12-31,aaa
44062120-d060-4c21-9cb5-0000000001,Tom,Osaka,46,2019-01-01,aaa
44062120-d060-4c21-9cb5-0000000002,Kevin,Fukuoka,72,2023-08-15,aaa
44062120-d060-4c21-9cb5-0000000003,Smith,Tokyo,73,2020-01-29,aaa
44062120-d060-4c21-9cb5-0000000004,Mike,Osaka,60,2022-12-31,aaa
44062120-d060-4c21-9cb5-0000000005,Tom,Fukuoka,28,2019-01-01,aaa
44062120-d060-4c21-9cb5-0000000006,Kevin,Tokyo,52,2023-08-15,aaa
44062120-d060-4c21-9cb5-0000000007,Smith,Osaka,61,2020-01-29,aaa
44062120-d060-4c21-9cb5-0000000008,Mike,Fukuoka,79,2022-12-31,aaa
44062120-d060-4c21-9cb5-0000000009,Tom,Tokyo,32,2019-01-01,aaa
44062120-d060-4c21-9cb5-0000000010,Kevin,Osaka,71,2023-08-15,aaa
44062120-d060-4c21-9cb5-0000000011,Smith,Fukuoka,50,2020-01-29,aaa
44062120-d060-4c21-9cb5-0000000012,Mike,Tokyo,35,2022-12-31,aaa

関連記事

Python ~ 基本編 / CSV
https://dk521123.hatenablog.com/entry/2019/11/07/214108
Python ~ 基本編 / 文字列 ~
https://dk521123.hatenablog.com/entry/2019/10/12/075251
Python ~ 基本編 / 日付・日時 ~
https://dk521123.hatenablog.com/entry/2019/10/14/121909
Python ~ 基本編 / 繰り返し ~
https://dk521123.hatenablog.com/entry/2019/08/25/000330
Python ~ 基本編 / 条件分岐 ~
https://dk521123.hatenablog.com/entry/2020/07/17/000000
Python ~ 基本編 / リスト ~
https://dk521123.hatenablog.com/entry/2019/10/12/084943
Pythonで セキュアなランダム文字列生成を考える
https://dk521123.hatenablog.com/entry/2021/11/29/110408
JS で ファイル出力するCSVデータ生成ツールを作ってみる
https://dk521123.hatenablog.com/entry/2022/11/28/000000
Python ~ 非同期 / concurrent.futures ~
https://dk521123.hatenablog.com/entry/2023/04/19/232949