からっぽの日記
2007-05-29(火) いろんなニュースがありましたね... [長年日記]
■ Mona
ひげぽん OSとか作っちゃうかMona- - Mona 0.3.0 alpha9 リリース
Mona 、すげー*1。以前見たときよりだいぶ進化してるような。Schemeシェルとか楽しそう、Lisp系は書けませんが(゜∀゜ ) 一度入れてみよっかな。
*1 見た目のみで感激してます。ごめんなさい。
■ 複数の文字コードが混ざったファイル
Shift_JIS と EUC-JP が混ざったファイ(行単位)ルが出来ていたので、Shift_JIS に統一したいと思い簡単な文字コードのエンコード処理を書いてみた。
まずRuby では、
require 'nkf' # エンコード処理前 input_file = "hoge.txt" # エンコード処理後 output_file = "fuga.txt" lines = [] File.open(input_file) do |f| f.each do |line| lines << NKF.nkf("-s", line) end end File.open(output_file, "w") do |f| lines.each do |line| f.puts line end end
無事変換完了。
しかし、今回は Ruby ではあまり都合がよくないので 多少書いてる C# でやってみる。
using System; using System.Text; using System.IO; using System.Collections; class Hoge { static void Main(string[] args) { string inputFile = "hoge.txt"; string ouptputFile = "fuga.txt"; string line = ""; Encoding sjisEnc = Encoding.GetEncoding("Shift_JIS"); using (StreamWriter sw = new StreamWriter(ouptputFile, false, Encoding.GetEncoding("Shift_JIS"))) { using (StreamReader sr = new StreamReader(inputFile, Encoding.GetEncoding("Shift_JIS"))) { while((line = sr.ReadLine()) != null) { byte[] bytes = sjisEnc.GetBytes(line); string encodedStr = sjisEnc.GetString(bytes); sw.WriteLine(encodedStr); } } } } }
EUC-JPの箇所がうまく変換できない。StreamReader でファイルを開くときにおかしくなる?適当にエンコードをいろいろ変えても文字コードが統一ができなかった。C# の文字コードの扱いってどうなってるんやろ?Ruby なんでできるんやろ?
[ツッコミを入れる]