VOOZH about

URL: https://hu.wikipedia.org/wiki/Modul:CSV

⇱ Modul:CSV – Wikipédia


Ugrás a tartalomhoz
Ellenőrzött
A Wikipédiából, a szabad enciklopédiából
Ez a közzétett változat, ellenőrizve: 2013. május 16.
Pontosságellenőrzött

👁 Image
CSV[mi ez?] • [dokumentáció: mutat, szerkeszt] • [tesztek: létrehozás]

localp={}
default_settings={
delimiter=',',
quotechar='"',
ignoreEmptyLines=true,
--linebreak deviates from RFC4180, where the default linebreak is CRLF. Mediawiki newlines are linebreaks
linebreak='\n'
}
--datastring is the string containing the CSV data. settings follows the format of default_settings
--returns a table which is a squence of sequeces of fields
functionparse(datastring,settings)
ifsettings==nilthensettings={}end
iftype(settings)~='table'thenreturn{},"Error, settings is not a table"end
fork,vinpairs(default_settings)do
ifsettings[k]==nilthensettings[k]=default_settings[k]end
end
returnreaddata(datastring,settings)
end
functionreaddata(datastring,settings)
datastring=datastring:gsub(settings.linebreak,settings.linebreak..settings.delimiter)..settings.delimiter;
localitem_list={};
forvindatastring:gmatch("([^"..settings.delimiter.."]*)"..settings.delimiter)do
table.insert(item_list,v);
end
localitem_list2={};
localcurrent=''
localinquote=false
for_,vinipairs(item_list)do
ifnotinquotethen
ifv:match("^%s*"..settings.quotechar)then
inquote=true;
current=v;
else
table.insert(item_list2,v);
end
else
current=current..settings.delimiter..v;
end
ifinquotethen
ifv:match(settings.quotechar.."%s*$")then
current=current:match("^%s*"..settings.quotechar.."(.*)"..settings.quotechar.."%s*$");
current=current:gsub(settings.linebreak..settings.delimiter,settings.linebreak);
current=current:gsub(settings.quotechar..settings.quotechar,settings.quotechar);
table.insert(item_list2,current);
inquote=false;
current='';
end
end
end
ifcurrent~=''then
table.insert(item_list2,current);
end
localrows={};
item_list={};
localfront=''
for_,vinipairs(item_list2)do
front=v:match("(.*)"..settings.linebreak.."$");
iffront~=nilthen
if#item_list>0orfront:len()>0ornotsettings.ignoreEmptyLinesthen
table.insert(item_list,front)
table.insert(rows,item_list);
end
item_list={};
else
table.insert(item_list,v);
end
end
if#item_list>0then
table.insert(rows,item_list);
end
returnrows;
end
p.parse=parse
returnp