--
-- 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. SSM框架搭建总结(2)

    一.开发环境搭建 1.本地jdk安装及配置环境变量 2.本地tomcat安装 3.本地maven安装 3.1 maven安装 3.2 maven数据仓库配置settings.xml 4.本地安装SVN ...

  2. OpenWrt/LEDE配置OpenVPN Server

    安装openvpn不再赘述 准备证书文件主要有以下文件: (服务端)ca.crtdh2048.pemserver.crtserver.key(客户端)ca.crtclient01.keydp-clie ...

  3. android应用Theme(二)

    另外一种实现android应用Theme的方式是通过apk来实现的. 以下是一个demo. 1.首先必须新建一个apk.类似的插件,然后在该apk的AndroidManifest.xml文件的appl ...

  4. JS正则替换掉小括号及内容

    正則表達式:\ ( [ ^ \ ) ] * \ ) JS代码: var str="hello(world)"; var nstr = str.replace(/\([^\)]*\) ...

  5. 【转】centos(原生yum系通用)安装xfce便捷方法

    一个鸟人突然来了句他要在centos 5.2上装xfce,yum install xfce和yum install xfce4均没有合适的包(服务器没装x环境)于是我ssh登录到服务器上看了下,的确没 ...

  6. RHEL/CentOS 7.x/6.x/5.x开启EPEL仓库

    说明 原文链接 翻译:@adolphlwq 项目地址 这篇指南文章教你如何在 RHEL/CentOS 7.x/6.x/5.x 系统中开启EPEL仓库支持,以便你可以使用 yum 命令 安装额外的标准开 ...

  7. No package的问题解决

    更新pecl扩展 yum  install epel-release  //扩展包更新包yum  update //更新yum源

  8. 基于js利用经纬度进行两地的距离计算(转)

    转自:http://www.storyday.com/html/y2009/2212_according-to-latitude-and-longitude-distance-calculation- ...

  9. linux 文件系统工作原理

    转:<http://linuxperf.com/?p=153> 一.概述 文件系统要解决的一个关键问题是怎样防止掉电或系统崩溃造成数据损坏,在此类意外事件中,导致文件系统损坏的根本原因在于 ...

  10. android在不加载图片的前提下获得图片的宽高

    public static int[] getImageWidthHeight(String path){ BitmapFactory.Options options = new BitmapFact ...