0
0

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 3 years have passed since last update.

Loopを使ったCSVファイルの文字コード変換(Shift JISからUTF8)

Posted at

以下のような連続するファイル名を持つCVSファイルがあるとする:

  • brabra_01.csv
  • brabra_02.csv
  • brabra_03.csv
  • (...)
  • brabra_47.csv

元ファイルの文字コードがShift-JISで、これらをUTF-8に一括変換する。さらにファイルの連続番号のゼロ詰めをなくし、以下のようにリネームする:

  • brabra_1_utf.csv
  • brabra_2_utf.csv
  • brabra_3_utf.csv
  • (...)
  • brabra_47_utf.csv

コードは以下の通り。

ChangeShiftJIS2utf8.ipynb
import csv
import codecs
a = 47
for i in range(a):
    pref = i + 1
    path = "c:/*****/brabra_%02.f.csv"%pref
    path_utf = "C:/****/brabra_%01.f_utf.csv"%pref
    fin = codecs.open(path, "r", "shift_jis")

    fout_utf = codecs.open(path_utf, "w", "utf-8")
    
    for row in fin:
        fout_utf.write(row)
        
    fin.close()
    fout_utf.close()
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?