## tinyCSV: Now handling CSV is a child's play

YoungNeer
Party member
Posts: 118
Joined: Wed May 15, 2019 7:49 am

### tinyCSV: Now handling CSV is a child's play

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.
Note the library at github, you can fork it and make your own changes
https://github.com/YoungNeer/lovelib/tr ... er/tinyCSV
So basically tinyCSV works like this:
You make a file object which would be used to read and write files

Code: Select all

	csvfile=require 'tinyCSV'

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

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)

Generally speaking parse actually accepts 3 parameters:-

Code: Select all

	csvfile:parse(path,delimiter,format)
--note that delimiter is '=' and format is 1 (i.e. [key]=value format) by default

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)

Code: Select all

	csvfile:write(filename,tbl,delimiter,format)

Note that would truncate the file so if you want to append file then use append function- (which has same arglist as write)

Code: Select all

	csvfile:append(filename,tbl,delimiter,format)

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)
My Github- your contribution is highly appreciated

### Who is online

Users browsing this forum: No registered users and 29 guests