Last active
September 6, 2016 09:18
-
-
Save kashewnuts/030480510294b7fb617b431de118911e to your computer and use it in GitHub Desktop.
csv_split.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import csv | |
import sys | |
import time | |
''' | |
CSV形式のファイルでISBN13の末尾に応じてファイル作成 | |
''' | |
FILENAME = 'M_Product' | |
FILETYPE = '.txt' | |
def write_csv(row, lastIsbn13char): | |
''' | |
ISBN13コードの末尾文字に応じたファイルにCSV形式で読み込んだデータを追記。 | |
:param list row: 出力行 | |
:param str lastIsbn13char: ISBN13コードの末尾文字 | |
''' | |
with open(FILENAME + lastIsbn13char + FILETYPE, 'a') as f: | |
writer = csv.writer(f, lineterminator='\n') | |
writer.writerow(row) | |
def main(): | |
''' | |
モジュールを実行した際のエントリポイント。 | |
''' | |
start = time.time() | |
print('started make file.') | |
with open(FILENAME + FILETYPE, 'rt') as f: | |
try: | |
reader = csv.reader(f) | |
for row in reader: | |
# rowの先頭の要素がISBN13固定なので直接指定 | |
write_csv(row, str(row[0][-1])) | |
except csv.Error as e: | |
sys.exit('file %s, line %d: %s' % (f, reader.line_num, e)) | |
elapsed_time = time.time() - start | |
print('finished!: {0}'.format(elapsed_time) + '[sec]') | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment