LoginSignup
1
0

More than 1 year has passed since last update.

Juliaで学ぶプログラミング エンコード・デコード

Posted at

テキストファイルの操作とエンコード・デコード

ファイル操作では読み取りしたもののデータ形式を意識することが必要です。

ポイント

  • SHIF-JIS等異なる形式の テキストデーターの扱い
  • バイト文字列とテキストへの相互変換

青空文庫からデータを読み込み UTF-8で保存しなさい。

#using Pkg
#Pkg.add("ZipFile")
#Pkg.add("StringEncodings")
using ZipFile
using StringEncodings
downloadUrl_str = "https://www.aozora.gr.jp/cards/000081/files/43754_ruby_17594.zip"
download( downloadUrl_str, "chumonNoOi.zip" )

r = ZipFile.Reader("chumonNoOi.zip")
for f in r.files
       println("Filename: $(f.name)")
       text_strAry = readlines(f,enc"CP932") #cp932 = shift-jis
        open( f.name, "w" ) do wf
            for text_str in text_strAry 
              println( wf, text_str )
            end
        end
end
close(r)

先程のUTF-8のテキストを読み込み、最初の3行を画面出力しなさい

fileName_str = "chumonno_oi_ryoriten.txt"
open( fileName_str, "r" ) do f
           line_strAry = readlines(f)
           println(line_strAry[1:3])
end
["注文の多い料理店", "宮沢賢治", ""]

UTF-8のテキストを読み込み、JISでファイル出力しなさい

fileName_str = "chumonno_oi_ryoriten.txt"
open( fileName_str, "r" ) do f
    line_strAry = readlines(f)
    text_str = join(line_strAry,"\n")    
    open("chumonno_oi_ryoriten_shiftJis.txt","w") do wf
        write(wf,encode(text_str,"shift-jis"))
    end
end

JISのテキストを読み込み、最初の2行を画面出力しなさい

以下画面出力のことを出力、画面以外の場合はXX出力と書きます。

fileName_str = "chumonno_oi_ryoriten_shiftJIs.txt"
open( fileName_str, "r" ) do f
    line_strAry = readlines(f,enc"shift-jis")
    println(line_strAry[1:2])
end
["注文の多い料理店", "宮沢賢治"]

テキストを読み込みバイト文字で最初の10個のデータを出力しなさい。

fileName_str = "chumonno_oi_ryoriten_shiftJIs.txt" #chumonno_oi_ryoriten.txtでも良い。その場合出力は異なる。
open( fileName_str, "r" ) do f
    data = read(f,enc"shift-jis")
    println(data[1:10])
end
UInt8[0xe6, 0xb3, 0xa8, 0xe6, 0x96, 0x87, 0xe3, 0x81, 0xae, 0xe5]

バイト化文字列を読み込みテキストに変換し出力しなさい。

fileName_str = "chumonno_oi_ryoriten_shiftJIs.txt" 
open( fileName_str, "r" ) do f
    data = read(f)
    s = decode(data, "shift-jis") 
    println(s)
end

デコードで文字コード指定はUTF-8であっても省略できない。

fileName_str = "chumonno_oi_ryoriten.txt" 
open( fileName_str, "r" ) do f
    data = read(f)
    s = decode(data,"utf-8") #デコードでは"utf-8"の指定を省略できない。
    println(s)
end
注文の多い料理店
宮沢賢治

-------------------------------------------------------
【テキスト中に現れる記号について】

《》:ルビ
(例)紳士《しんし》

|:ルビの付く文字列の始まりを特定する記号
(例)二|疋《ひき》

[#]:入力者注 主に外字の説明や、傍点の位置の指定
(例)[#ここから4字下げ、横書き、中央揃え、罫囲み]
-------------------------------------------------------

 二人の若い紳士《しんし》が、すっかりイギリスの兵隊のか
(以下省略)

テキストを1行だけ読み込み出力しなさい

fileName_str = "chumonno_oi_ryoriten_shiftJIs.txt"
open( fileName_str, "r" ) do f
    line_str = readline(f,enc"shift-jis")
    println(line_str)
end
注文の多い料理店

備考 ちなみにreadlineはイテレーター

「注文の多い料理店」をUtf-8でバイト化し、出力しなさい。

text_str = "注文の多い料理店"
println(encode(text_str,"utf-8"))
UInt8[0xe6, 0xb3, 0xa8, 0xe6, 0x96, 0x87, 0xe3, 0x81, 0xae, 0xe5, 0xa4, 0x9a, 0xe3, 0x81, 0x84, 0xe6, 0x96, 0x99, 0xe7, 0x90, 0x86, 0xe5, 0xba, 0x97]

解説

  • read(path)はバイト文字列になる
  • read(path, String, enc"UTF-16")など文字コードを指定することでテキスト読み込みできる
  • readline,readlinesはテキストで読み込み
  • read,write(line,lines含む)はenc"文字コード名"でutf-8以外を指定できる
  • decodeでバイト文字列からテキストへ変換
  • encodeでテキストからバイト文字列へ変換
1
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
1
0