Skip to content

Instantly share code, notes, and snippets.

@peace098beat
Created December 22, 2015 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peace098beat/696e741f941801ed705e to your computer and use it in GitHub Desktop.
Save peace098beat/696e741f941801ed705e to your computer and use it in GitHub Desktop.
[Python] CSVのかきだし 
def save(self, filename=None):
"""
評価結果をcsvで保存する
"""
if filename is None:
filename = 'result.csv'
filename_ = os.path.normpath(filename)
# OSを判定し、文字コードを取得
if os.name is 'nt':
code = 'cp932'
else:
code = 'utf-8'
with open(filename_, 'wb') as f:
# -- cav.writerの宣言 ----------------------------
writer = csv.writer(f, delimiter=',')
# ascii対応
# 文字列(UNICODE)は、バイト列に変換してあげないといけない
# utf-8ではEXCELで文字化けしてしまう。
# -- 評価の集計結果を表示
# name, 評価1, 評価2, 評価3
# V1X, 1, 5, 19
# V1, 11, 5, 19
# ...
# 見出しの書き出し
row = []
row.extend(['name'])
for n in range(self.evalution_num):
row.extend([self.evalution_s[n].encode(code), ])
writer.writerow(row)
for i in range(len(self.wav_filepath_s)):
# 書き出す一行を生成しておく
row = []
# ファイル名を後続列に追加
row = [os.path.basename(self.wav_filepath_s[i])]
# 値を後続列に追加
for n in range(self.evalution_num):
result_table = self.result_table_s[n]
s = sum(result_table[i])
row.extend([s])
# 行を書き込み
writer.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment