--
-- Author: My Name
-- Date: 2013-12-16 18:52:11
-- csv解析
-- -- 去掉字符串左空白
local function trim_left(s)
return string.gsub(s, "^%s+", "");
end -- 去掉字符串右空白
local function trim_right(s)
return string.gsub(s, "%s+$", "");
end -- 解析一行
local function parseline(line)
local ret = {}; local s = line .. ","; -- 添加逗号,保证能得到最后一个字段 while (s ~= "") do
--print(0,s);
local v = "";
local tl = true;
local tr = true; while(s ~= "" and string.find(s, "^,") == nil) do
--print(1,s);
if(string.find(s, "^\"")) then
local _,_,vx,vz = string.find(s, "^\"(.-)\"(.*)");
--print(2,vx,vz);
if(vx == nil) then
return nil; -- 不完整的一行
end -- 引号开头的不去空白
if(v == "") then
tl = false;
end v = v..vx;
s = vz; --print(3,v,s); while(string.find(s, "^\"")) do
local _,_,vx,vz = string.find(s, "^\"(.-)\"(.*)");
--print(4,vx,vz);
if(vx == nil) then
return nil;
end v = v.."\""..vx;
s = vz;
--print(5,v,s);
end tr = true;
else
local _,_,vx,vz = string.find(s, "^(.-)([,\"].*)");
--print(6,vx,vz);
if(vx~=nil) then
v = v..vx;
s = vz;
else
v = v..s;
s = "";
end
--print(7,v,s); tr = false;
end
end if(tl) then v = trim_left(v); end
if(tr) then v = trim_right(v); end ret[table.getn(ret)+] = v;
--print(8,"ret["..table.getn(ret).."]=".."\""..v.."\""); if(string.find(s, "^,")) then
s = string.gsub(s,"^,", "");
end end return ret;
end --解析csv文件的每一行
local function getRowContent(file)
local content; local check = false
local count =
while true do
local t = file:read()
if not t then if count== then check = true end break end if not content then
content = t
else
content = content..t
end local i =
while true do
local index = string.find(t, "\"", i)
if not index then break end
i = index +
count = count +
end if count % == then check = true break end
end if not check then assert(~=) end
return content
end --解析csv文件
function LoadCsv(fileName)
local ret = {}; local file = io.open(fileName, "r")
assert(file)
local content = {}
while true do
local line = getRowContent(file)
if not line then break end
table.insert(content, line)
end for k,v in pairs(content) do
ret[table.getn(ret)+] = parseline(v);
end file:close() return ret
end function Csv2Lua(fileName, filePath, desFilePath )
local t = LoadCsv(filePath);
if t then
t = luautil.serialize(fileName,t);
end
if t then
luautil.writefile(t, desFilePath)
end
end --test
--local t= LoadCsv("csvtesttxt.csv")
--for k,v in pairs(t) do
-- local tt = v
-- local s = ""
-- for i,j in pairs(tt) do
-- s = string.format("%s,%s",s,j)
-- end
-- print ("",s)
--end

lua 工具类(一)的更多相关文章

  1. lua 工具类(二)

    local tonumber_ = tonumber function tonumber(v, base) end function toint(v) return math.round(tonumb ...

  2. Lua工具类

    1.打印table --一个用以打印table的函数 function print_r (t, name) print(pr(t,name)) end function pr (t, name, in ...

  3. lua学习:lua中“类”的实现

    在之前的面试遇到考用lua实现类的题目.现在就补补这块知识点. 我们都知道Lua中的table是一个对象.拥有状态,拥有self,拥有独立于创建者和创建地的生命周期. 一个类就是一个创建对象的模具.L ...

  4. Android 开源控件与常用开发框架开发工具类

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

  5. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  6. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  7. [转]Java常用工具类集合

    转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...

  8. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

  9. Guava库介绍之实用工具类

    作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...

随机推荐

  1. excel怎么在插入的方框上打勾

    本例主要介绍如何在excel中插入带对勾的方框.   工具/原料 Excel 操作步骤:  在编辑Excel表格模板时,比如说简历.人力信息登记表等,经常需要有一些可选项,如下例的婚姻状况就包括“有配 ...

  2. MySQL与OLAP:分析型SQL查询最佳实践探索

    搞点多维分析,糙快猛的解决方式就是使用ROLAP(关系型OLAP)了.数据经维度建模后存储在MySQL,ROLAP引擎(比方开源的Mondrian)负责将OLAP请求转化为SQL语句提交给数据库.OL ...

  3. 【php页面表单提交】form校验后再提交,非ajax提交

    form表单校验后,在执行提交动作: <form method="post" action="{:U('Custom/addmsg')}" id=&quo ...

  4. nginx常用超时设置

    客户度连接nginx超时, 建议5s内 接收客户端header超时, 默认60s, 如果60s内没有收到完整的http包头, 返回408 Syntax: client_header_timeout t ...

  5. CentOS vps下,安装xfce/KDE/GNOME桌面+vncserver进行远程管理

    CentOS vps下,安装xfce/KDE/GNOME桌面+vncserver进行远程管理 首先安装桌面环境,我选择的是xfce,轻量级桌面,小巧实用不占太多内存,(占用内存方面,xfce少于kde ...

  6. 【RS】Using graded implicit feedback for bayesian personalized ranking - 使用分级隐式反馈来进行贝叶斯个性化排序

    [论文标题]Using graded implicit feedback for bayesian personalized ranking (RecSys '14  recsys.ACM ) [论文 ...

  7. rsync配置及优化

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  8. 树莓派进阶之路 (034) - 基于linux的ftp脚本

    基于linux的ftp脚本: #!/bin/sh cd echo "彻底卸载原有的ftp" sudo apt-get remove --purge vsftpd #(--purge ...

  9. idea上activiti插件的安装及使用

    最近做的东西需要用到activiti,做个笔记 首先下载activti插件- actiBPM File - settings-plugins-Browse-Repositories 搜索 actiBP ...

  10. SpringBoot配置属性之其他

    SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...