2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python3でcsvファイルを読み込み、1行ずつファイルに分ける

Last updated at Posted at 2016-10-30

python3でcsvファイルを読み込み、1行ずつファイルに分ける
各行のidをファイル名に付ける

入力するcsvファイル

id, text
1,テキスト1
2,テキスト2
3,テキスト3
# -*- conding: utf-8 -*-

import csv

if __name__ == '__main__':

    file_out_path = 'hoge/'   

    with open('home.csv', 'rt', encoding="utf-8") as file_in:
        csv_in = csv.reader(file_in, delimiter=",")
        header = next(csv_in) #headerを削除
        for row in csv_in:
            with open(file_out_path + row[0] + '.txt', 'w') as file_out:
            file_out.write(row[1])
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?