lua 工具类(一)
--
-- 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 工具类(一)的更多相关文章
- lua 工具类(二)
local tonumber_ = tonumber function tonumber(v, base) end function toint(v) return math.round(tonumb ...
- Lua工具类
1.打印table --一个用以打印table的函数 function print_r (t, name) print(pr(t,name)) end function pr (t, name, in ...
- lua学习:lua中“类”的实现
在之前的面试遇到考用lua实现类的题目.现在就补补这块知识点. 我们都知道Lua中的table是一个对象.拥有状态,拥有self,拥有独立于创建者和创建地的生命周期. 一个类就是一个创建对象的模具.L ...
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Android—关于自定义对话框的工具类
开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...
- [转]Java常用工具类集合
转自:http://blog.csdn.net/justdb/article/details/8653166 数据库连接工具类——仅仅获得连接对象 ConnDB.java package com.ut ...
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
- Guava库介绍之实用工具类
作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 本文是我写的Google开源的Java编程库Guava系列之一,主要介 ...
随机推荐
- Linux命令行监控网口流量
1.ifconfig [网卡名] ifconfig eth0 . 2. /proc/net/dev cat /proc/net/dev . 3. 使用watch命令配合 ifconfig, cat / ...
- 微信扫码支付springboot版本
发布时间:2018-11-06 技术:springboot+freemarker 概述 该项目是一个采用springboot构建的web项目,主要实现了微信扫码支付功能.包含最基本的创建订单, ...
- CSS的子选择器与后代选择器的区别
来源于:http://www.jianshu.com/p/599654ba5f4a 子选择器: 一个比较有用的选择器子选择器,即大于符号(>),用于选择指定标签元素的第一代子元素.如右侧代码编辑 ...
- JavaScript判断是否全为中文,是否含有中文
来源于:http://blog.csdn.net/yenange/article/details/7463897 第一种代码(全为中文则返回"true",不全为中文则返回" ...
- hduoj1073--Online Judge
做道题,并没有太多的技巧,关键在与对Accepted,presented error 和wa的判断,第一步如果两者完全一样,那么很定是AC了 ,否则如果去掉多余换行,空格,制表后还有不同说明是数据 不 ...
- session会话保持原理
1. 什么是会话保持? 会话保持是负载均衡最常见的问题之一,也是一个相对比较复杂的问题.会话保持有时候又叫做粘滞会话(Sticky Sessions).会话保持是指在负载均衡器上的一种机制,可以识别客 ...
- 进阶之路(基础篇) - 009 通过底层AVR方法实现SPI数据传输
主机端: /********************************* 代码功能:通过底层AVR方法实现SPI数据传输(主机端) 创作时间:2016*10*17 使用资源: 更低阶的 aTme ...
- 微信支付中的jsapi返回提示信息
jsapi中跳转到微信支付中触发的方法是js中的getBrandWCPayRequest方法. 改方法中的返回结果msg提示信息如下: err_msg:get_brand_wcpay_request: ...
- SQL Server 表分区之水平表分区
什么是表分区? 表分区分为水平表分区和垂直表分区,水平表分区就是将一个具有大量数据的表,进行拆分为具有相同表结构的若干个表:而垂直表分区就是把一个拥有多个字段的表,根据需要进行拆分列,然后根据某一个字 ...
- Redis 启动警告错误解决[转]
Redis 启动警告错误解决 启动错误 1.WARNING overcommit_memory is set to 0! Background save may fail under low memo ...