0
0

More than 3 years have passed since last update.

Python3でBOM付UTF-8のCSV(Excelで直接読めるCSV)を出力する方法

Posted at

CSVファイルを文字コード UTF-8で作ると
Excelで読み込んだ時に文字化けする。
文字化けさせないためには先頭にBOMを付ける必要がある。

bom.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys

# BOMを出力する
sys.stdout.buffer.write(b'\xEF\xBB\xBF')
# CSVを出力する
print ('ID,名前')
print ('1,山田')
print ('2,田中')
print ('3,坂本')

これで

./bom.py > excel.csv

と実行するとExcelで文字化けしないCSVファイル(excel.csv)が生成される。

0
0
0

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