Created
July 21, 2015 05:59
-
-
Save sota1235/50ff502b00f1bf0e4e86 to your computer and use it in GitHub Desktop.
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/python | |
# -*- cofing: utf-8 -*- | |
import unicodedata | |
import sys | |
# 全角含んだ文字の幅を返す | |
def count_word_width(word): | |
width = 0 | |
for w in word: | |
if unicodedata.east_asian_width(w) is 'W': | |
width += 2 | |
else: | |
width += 1 | |
return width | |
# ファイル読み込み | |
filename = sys.argv[1] | |
data = [] | |
for line in open(filename, 'r'): | |
data.append(line.replace('\n', '').split(',')) | |
columns = len(data[0]) # カラム数 | |
# それぞれのカラムの最大値を取得 | |
max_width = [] | |
for i in range(columns): | |
elm = [] | |
for j in range(len(data)): | |
elm.append(count_word_width(data[j][i])) | |
max_width.append(max(elm)) | |
# ファイル出力 | |
f = open('./answer/output.txt', 'w') | |
for d in data: | |
wline = [] | |
for i in range(columns): | |
wline.append(d[i] + ' ' * (max_width[i] - count_word_width(d[i]))) | |
f.write('|' + '|'.join(wline) + '|\n') | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
全角含めた文字幅測ってcsvを整形するプログラム(新人課題)