ヘッダー付き、ダブルクォーテーション(二重引用符)囲いの CSV ファイルの作成方法です。
プログラム
csv_write.py
#! /usr/bin/python
#
# csv_write.py
#
# May/14/2024
#
import sys
import csv
#
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
file_out = sys.argv[1]
#
header=["市区町村コード","宛名番号","個人履歴番号","個人履歴番号_枝番号","最新フラグ"]
data=[["131016","12345678912","1","1","1"],
["131016","12345678913","1","1","1"],
["131016","12345678914","1","1","1"],
["131016","12345678915","1","1","1"],
["131016","12345678916","1","1","1"]]
#
with open(file_out, 'w') as ff:
writer = csv.writer(ff,delimiter=',',
quotechar='"',quoting=csv.QUOTE_ALL)
writer.writerow(header)
writer.writerows(data)
#
sys.stderr.write("*** 終了 ***\n")
# --------------------------------------------------------------------
実行方法
./csv_write.py out01.csv
作成された CSV ファイル
out01.csv
"市区町村コード","宛名番号","個人履歴番号","個人履歴番号_枝番号","最新フラグ"
"131016","12345678912","1","1","1"
"131016","12345678913","1","1","1"
"131016","12345678914","1","1","1"
"131016","12345678915","1","1","1"
"131016","12345678916","1","1","1"