0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

csvファイルを新規作成する

Posted at

csv ファイルを新規作成してヘッダとデータを書き込む

  1. import csv で csvライブラリを使用する。
  2. with open() で csvファイルを開いて、close忘れ防止。
  3. csv.writer() で writer を使ってヘッダやデータを書き込む。
import csv

# csv のヘッダ(列名)
header = [
    ['No.','Name','Point']
]

# csv の data
datas = [
    [ 1, 'Mike' , 99],
    [ 2, '山田' , 89],
    [ 3, 'Temp' , 5 ],
]

# csv ファイルを新規作成する。
with open("./sample.csv", mode="w", newline='', encoding="UTF-8") as file:

    # csv writer を取得する。(QUOTE_ALLで、全てクオート("")する)
    writer = csv.writer(file, delimiter='\t', quoting=csv.QUOTE_ALL)

    # ヘッダ(列名)を設定する。
    writer.writerows( header )

    # データを追加する。
    writer.writerows( datas )
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?