txt 开关 csv 可通用 工具

 2023-09-05 阅读 26 评论 0

摘要:Ruby Txt 开关 CSV 常用工具 to require 'csv'namespace :sys_file_conver dodesc "把txt数据导入到csv中"task :txt_to_csv => :environment doamount = 100head_hash = {'对账文件名' =>[12],'商户号' =>[15


Ruby Txt 开关 CSV 常用工具


to



require 'csv'namespace :sys_file_conver dodesc "把txt数据导入到csv中"task :txt_to_csv => :environment doamount = 100head_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,'wb:gbk:gbk') do |data| # wb:gbk:gbk to gbk default encoding is utf-8data << hash_to_array_value(head_hash)txt_array = IO.readlines(txt_file_path, :encoding => 'GB2312')txt_array.each_with_index do |str,index|case indexwhen 0data << str_to_array_by_hash(str,head_hash)data << hash_to_array_value(body_hash)elsedata << str_to_array_by_hash(str,body_hash)end  endendend
# ----------------------------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]}array
end# def hash_to_array_key(hash)
#   hash.inject([]){|a,d| a << convert_gbk(d[0])}
# end
def hash_to_array_value(hash,i=0)hash.inject([]){|a,d| a << convert_gbk(d[i])}
end# ---------------------------Convert util--------------------------------------  
def convert(str)
require 'iconv'
begin
converter = Iconv.new("UTF-8", "GB2312")
converter.iconv(str)
rescue
str
end
enddef convert_gbk(str)
require 'iconv'
begin
converter = Iconv.new("GB2312", "UTF-8")
converter.iconv(str)
rescue
str
end
endend

Html form tag

.upload-body= form_tag upload_file_file_converts_url, :multipart => true do%p%span.label 上传文件= file_field :public, :picture%p%input.button.ml20{ :type => 'submit', :value => '转码' } 



controller upload to download

  #iconv -f gbk csv_file.csv def upload_fileuploaded_io = params[:public][:picture] file = File.join("public", uploaded_io.original_filename)FileUtils.cp uploaded_io.path, filesend_file FileConvert.txt_convert_csv(file),:filename => "CSV-#{uploaded_io.original_filename} #{Time.now.strftime('%F %T')}.csv"end




版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/1430.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息