RSS
 

用ruby写了转txt编码的程序。gbk-》utf8

12 Sep
#!/usr/bin/env ruby
#require 'rubygems'
#require 'active_record'
require 'iconv'

input_path = 'input/'
output_path = 'output/'
if !File.directory? input_path
  path = File.expand_path(input_path)
  Dir.mkdir(input_path)
end
if !File.directory? output_path
  path = File.expand_path(output_path)
  Dir.mkdir(output_path)
end

docs = Dir.glob(input_path + '*')
#puts docs.inspect
for doc in docs do
  puts doc
  if File.file? doc
    puts 'in here'
    report = ''
    report_utf8 = ''
    File.open( doc,'r') do |f|
      report = f.read
    end
    begin
      puts 'start!'

      #name = File.split(path)[1]
      filename = output_path + File.basename(doc)
      puts filename

      #begin
      #  conv = Iconv.new("GBK", "utf-8")
      #  report_utf8 = conv.iconv(report)
      #rescue
      #end
      begin
        conv = Iconv.new("utf-8", "GBK")
        report_utf8 = conv.iconv(report)
      rescue
        puts File.basename(doc) + ' error'
      end
      puts 'ok'

      if report_utf8 && report_utf8 != ''
        if File.file?(filename)
          File.delete(filename)
        end

        File.open( filename, 'w') do |f|
          f.write report_utf8
        end
      end
    rescue
    end
  end
end
 
No Comments

Posted in 工作

 

Leave a Reply