tinyCSV: Now handling CSV is a child's play
Posted: Sun Jul 21, 2019 5:25 pm
Handling CSV's maybe pretty complicated if you start from scratch and there maybe some libraries out there that solves the problem. But I don't think most of them has the feature of storing data in more than one way.
And just-in-case if any-one here doesn't know what a CSV is then let me explain in brief. It's essentially a text-file with records seperated by comma. There can be all sorts of CSV's but the format that tinyCSV supports is
key=value (= is a delimiter which you can change to ':','-',etc but the overall format should be the same)
An example of a CSV file (that could be used with tinyCSV) is:-
robin=20,batman=120,joker=500,wonderwoman=0,superman=1000
And note that whitespaces don't matter for tinyCSV so you can have as many whitespaces (new lines,tabs,spaces,etc) between any two records.
You make a file object which would be used to read and write files
And then you decide the way data should be stored
There are TWO WAYS of storing data-
1. [key]=value format (advice: use when key is unique such as in {WWIDTH=1280,WHEIGHT=720}
2. {key,value} format (advice: use when key may not be unique such as in {robin=100,jack=20,robin=20})
Don't worry if you don't understand it straight ahead - there are examples with the FULL DOCUMENTATION at the github link)
Once you have decided the way data should be stored you can move to reading a file
The most basic way to read a file is
Generally speaking parse actually accepts 3 parameters:-
There are few other read functions both they are essentially same as parse just with different default argument so they are not discussed here.
Now to write a file is pretty simple. You pass in the filename,table, delimiter ('=' by default) and format (1 by default)
Note that would truncate the file so if you want to append file then use append function- (which has same arglist as write)
NOTE WHEN USING append YOU MUST REQUIRE iTable BEFORE REQUIRING tinyCSV
(https://love2d.org/forums/viewtopic.php?f=5&t=86906)
There are other functions as well but most of them are too trivial to be listed here (that is not to say they are not useful - infact the one's I left behind are I guess more useful cause i assume you will mostly read or write at the default love's save location)
And just-in-case if any-one here doesn't know what a CSV is then let me explain in brief. It's essentially a text-file with records seperated by comma. There can be all sorts of CSV's but the format that tinyCSV supports is
key=value (= is a delimiter which you can change to ':','-',etc but the overall format should be the same)
An example of a CSV file (that could be used with tinyCSV) is:-
robin=20,batman=120,joker=500,wonderwoman=0,superman=1000
And note that whitespaces don't matter for tinyCSV so you can have as many whitespaces (new lines,tabs,spaces,etc) between any two records.
So basically tinyCSV works like this:Note the library at github, you can fork it and make your own changes
https://github.com/YoungNeer/lovelib/tr ... er/tinyCSV
(I'll most likely accept your contribution and if I do I will add your name to the header)
You make a file object which would be used to read and write files
Code: Select all
csvfile=require 'tinyCSV'
There are TWO WAYS of storing data-
1. [key]=value format (advice: use when key is unique such as in {WWIDTH=1280,WHEIGHT=720}
2. {key,value} format (advice: use when key may not be unique such as in {robin=100,jack=20,robin=20})
Don't worry if you don't understand it straight ahead - there are examples with the FULL DOCUMENTATION at the github link)
Once you have decided the way data should be stored you can move to reading a file
The most basic way to read a file is
Code: Select all
tbl=csvfile:parse('yourfile.csv')
--assuming the 'yourfile.csv' has the delimiter '=' and we want the [key]=value format (i.e. format 1)
Code: Select all
csvfile:parse(path,delimiter,format)
--note that delimiter is '=' and format is 1 (i.e. [key]=value format) by default
Now to write a file is pretty simple. You pass in the filename,table, delimiter ('=' by default) and format (1 by default)
Code: Select all
csvfile:write(filename,tbl,delimiter,format)
Code: Select all
csvfile:append(filename,tbl,delimiter,format)
(https://love2d.org/forums/viewtopic.php?f=5&t=86906)
There are other functions as well but most of them are too trivial to be listed here (that is not to say they are not useful - infact the one's I left behind are I guess more useful cause i assume you will mostly read or write at the default love's save location)