csvファイルをhtmlのtableにしたい
この
Excelで作って
utf-8で保存した
csvファイルを
司書.csv
1,修繕I,10
2,耐久力III,22
3,効率V,64
こうしたい
liberarian.html
<tr>
<td class='id'>1</td>
<td class='enchant'>修繕I</td>
<td class='emerald'>10</td>
</tr>
<tr>
<td class='id'>2</td>
<td class='enchant'>耐久力III</td>
<td class='emerald'>22</td>
</tr>
<tr>
<td class='id'>3</td>
<td class='enchant'>効率V</td>
<td class='emerald'>64</td>
</tr>
探せばツールとかありそうだが
今回はpythonを使って
csvファイルのカンマをなくして
htmlのタグを付ける
readfile = "司書.csv" #読み込みファイル
writefile = "liberarian.html" #書き出しファイル
table = list() #書き出し用リスト
#ファイル読み込み
with open(readfile, mode="r", encoding="utf-8-sig") as f:
lines = f.read().splitlines()
#1行ずつ切り分けてタブをつける
for l in lines:
str = l.split(",")
t = "<tr>\n <td class='id'>" + str[0] + "</td>\n <td class='enchant'>" + str[1] + "</td>\n <td class='emerald'>" + str[2] + "</td>\n</tr>\n"
table.append(t)
#ファイル書き出し
with open(writefile, mode="w", encoding="utf-8") as f:
f.writelines(table)
あとは
あれこれすれば表にできる。手動で