txt轉為csv可以通用的工具

來源:互聯網
上載者:User

Ruby Txt 轉為 CSV 通用的工具

require 'csv'namespace :sys_file_conver do  desc "把txt資料匯入到csv中"  task :txt_to_csv => :environment do      amount = 100      head_hash = {        '對賬檔案名稱'  =>[12],        '商戶號'       =>[15],        '商戶類型'     => [4],        '清算日期'     => [8],        '總比數'       => [12],        '總金額'       => [15, amount],        '支付總比數'    => [12],        '支付總金額'    => [15, amount],        '退款總比數'    => [12],        '退款總金額'    => [15, amount],        '撤銷總比數'    => [12],        '撤銷總金額'    => [15, amount]      }      body_hash = {        '清算日期'    =>  [8],        '入賬日期'    =>  [8],        '卡號'        =>  [19],        '交易類型'    => [4],        '交易金額'    => [ 15, amount],        '入賬金額'    => [ 15, amount],        '手續約金額'  => [ 15, amount],        '流水號'     => [6],        '交易日期時間'  => [10],        '終端號'     => [8],        '商戶號'     => [15],        '商戶類型'    => [4],        '受理機構號'  => [6],        '系統檢索號'  => [12],        '授權號'     => [6],        '渠道類型'    => [2],        '支付卡種'    => [2],        '訂單號'     => [40]      }      txt_file_path = ENV['txt_file_path'] || "#{Rails.root}/tmp/txt_file.txt"      csv_file_path = ENV['csv_file_path'] || "#{Rails.root}/tmp/csv_file.csv"      File.new(csv_file_path) if FileTest::exists?(csv_file_path)      CSV.open(csv_file_path,'w') do |data|        data << hash_to_array_value(head_hash)        txt_array = IO.readlines(txt_file_path, :encoding => 'GB2312')        txt_array.each_with_index do |str,index|          case index          when 0            data << str_to_array_by_hash(str,head_hash)            data << hash_to_array_value(body_hash)          else            data << str_to_array_by_hash(str,body_hash)          end          end      end  end# ----------------------------Hash to array------------------------------------def str_to_array_by_hash(str,hash)  array = []  hash_to_array_value(hash, 1).inject(0){ |idx, code|    str_code = str[idx,code[0]]    str_code = str_code.to_f/code[1] unless code[1].blank?    array << convert_gbk(str_code)    idx+code[0]  }  arrayend# def hash_to_array_key(hash)#   hash.inject([]){|a,d| a << convert_gbk(d[0])}# enddef hash_to_array_value(hash,i=0)  hash.inject([]){|a,d| a << convert_gbk(d[i])}end# ---------------------------Convert util--------------------------------------  def convert(str)require 'iconv'beginconverter = Iconv.new("UTF-8", "GB2312")converter.iconv(str)rescuestrendenddef convert_gbk(str)require 'iconv'beginconverter = Iconv.new("GB2312", "UTF-8")converter.iconv(str)rescuestrendendend

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.