Ruby Txt 开关 CSV 常用工具

to

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,'wb:gbk:gbk') do |data| # wb:gbk:gbk to gbk default encoding is utf-8
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]
} 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
end def convert_gbk(str)
require 'iconv'
begin
converter = Iconv.new("GB2312", "UTF-8")
converter.iconv(str)
rescue
str
end
end end

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_file
uploaded_io = params[:public][:picture] file = File.join("public", uploaded_io.original_filename)
FileUtils.cp uploaded_io.path, file
send_file FileConvert.txt_convert_csv(file),:filename => "CSV-#{uploaded_io.original_filename} #{Time.now.strftime('%F %T')}.csv"
end

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

txt 开关 csv 可通用 工具的更多相关文章

  1. 【游戏开发】Excel表格批量转换成CSV的小工具

    一.前言 在工作的过程中,我们有时可能会面临将Excel表格转换成CSV格式文件的需求.这尤其在游戏开发中体现的最为明显,策划的数据文档大多是一些Excel表格,且不说这些表格在游戏中读取的速度,但就 ...

  2. txt文件按行处理工具类(可以截取小说、分析日志等)【我】

    txt文件按行处理工具类(可以分析日志.截取小说等) package file; import java.io.BufferedReader; import java.io.BufferedWrite ...

  3. 预读(读取文件前几行)文件(txt,dat,csv等)程序

    需求: txt.dat.csv文件很大,需要花很长的时间打开, 但实际上我们只需要查看文件的前几行,查看数据的内容和格式 exe & code :  https://github.com/co ...

  4. save tracking results into csv file for oxuva long-term tracking dataset (from txt to csv)

    save tracking results into csv file for oxuva long-term tracking dataset (from txt to csv) 2019-10-2 ...

  5. solr6.6 导入 pdf/doc/txt/json/csv/xml文件

    文本主要介绍通过solr界面dataimport工具导入文件,包括pdf.doc.txt .json.csv.xml等文件,看索引结果有什么不同.其实关键是managed-schema.solrcon ...

  6. python读取txt、csv、xml

    读取txt,无需引入任何包: user_file = open('user_info.txt','r') lines = user_file.readlines() user_file.close() ...

  7. mysql将表数据导出为txt或csv文件

    语法:select 字段 from 表名 into outfile 路径 示例txt:select * from stu_class into outfile './stu_class.text'; ...

  8. 20171026python读取txt写入csv

    import csv def get_lines(filepath): with open(filepath) as file_object: lines=set(file_object.readli ...

  9. R语言学习——R读取txt、csv、xls和xlsx格式文件

    最近项目中运用到了R读取文件数据,所以把相关好用的.经过验证的方法总结了一下,有效避免下次入坑. 1. R读取txt文件 使用R读取txt文件直接使用read.table()方法进行读取即可,不需要加 ...

随机推荐

  1. Sails.js中文文档

    Sails.js中文文档   http://sailsdoc.swift.ren/ Sails.js是一个Web框架,可以于轻松构建自定义,企业级Node.js Apps.它在设计上类似于像Ruby ...

  2. 妈蛋:kinMaxShow旋转木马异常,WebUploader图片上传坑爹,图像被压缩

    今天晚上在改造轮播图. 原来的代码是这种: <div> <img src="${static}/image/index/banner/`.jpg" /> & ...

  3. CentOS在安装配置 Ngnix_tomcat_PHP_Mysql

    安装Nginx yum install nginx 假设显示找不到 nginx包,新建一个文件/etc/yum.repos.d/nginx.repo,内容: [nginx] name=nginx re ...

  4. JMS ActiveMQ研究文档

    1. 背景 当前,CORBA.DCOM.RMI等RPC中间件技术已广泛应用于各个领域.但是面对规模和复杂度都越来越高的分布式系统,这些技术也显示出其局限性:(1)同步通信:客户发出调用后,必须等待服务 ...

  5. 类似的微博推断客户关系sql声明

    类别似新浪微博的关注和共同关心 不知道别人是怎么设计的. 反正我是例如以下设计的 ID   USER   FRIEND 1     A         B 2      B         A 3   ...

  6. InputMonitor注意事项

    文章只记录自己的点点理解.为了你自己的参考. 1.mInputFocus WMS.addWindow()-->WMS.finishUpdateFocusedWindowAfterAssignLa ...

  7. JAVA学习篇--JSTL基金会

    JSTL什么 JSTL(JSP Standard TagLibrary,JSP标准标签库)是一个不断完好的开放源码的JSP标签库. 为什么要用JSTL 我们JSP用于开发信息展现页很方便;也能够嵌入j ...

  8. POJ--2289--Jamie&#39;s Contact Groups【二分图的多个匹配+二分法答案】

    链接:id=2289">http://poj.org/problem?id=2289 意甲冠军:有n个人,m个分组,每一个人能够分配到一些组别.问怎样分能使得人数最多的组别人数最少. ...

  9. 于ubuntu-kylin14.10下一个,无法使用apt-get具( libc6-i386 : 赖: libc6 (= 2.15-0ubuntu10.5) 但 2.19-0ubuntu6 一个已)

    这篇文章有xhz1234(徐洪志)书写.转载请注明出处. http://blog.csdn.net/xhz1234/article/details/37044531 作者:徐洪志 背景:安装wine. ...

  10. VisualC++2012 Compiler Warning C4566

    现象: 今天敲代码突然遇到这样一个警告: warning C4566: ユニバーサル文字名 '\u0642' によって表示されている文字は.現在のコード ページ (932) で表示できません 意思是说 ...