LoginSignup
7
12

More than 5 years have passed since last update.

Excelで正しく開けるcsvをpythonで作るサンプル

Last updated at Posted at 2017-11-14
import codecs
import csv

# ファイルの拡張子は.csv
csv_filename = '/tmp/hoge.csv'

# 先頭にBOMを記載
with open(csv_filename, 'wb') as f:
  f.write(codecs.BOM_UTF16_LE)

# utf-16-leで追加書き込み。フォーマットはTSV
with codecs.open(csv_filename, 'ab', encoding='utf-16-le', errors='replace') as f:  
  tsv = csv.writer(f, delimiter="\t")
  tsv.writerows([
    ['Excelで', '正しく', '開ける'],
    ['csvを', 'pythonで', '作る'],
  ])

参考
https://qiita.com/mashiro/items/0d0b9bbb0f4a6cca620c

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