tinyCSV: Now handling CSV is a child's play

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
YoungNeer
Party member
Posts: 118
Joined: Wed May 15, 2019 7:49 am

tinyCSV: Now handling CSV is a child's play

Post by YoungNeer »

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
(I'll most likely accept your contribution and if I do I will add your name to the header)
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
kuinashi101
Prole
Posts: 7
Joined: Tue Feb 21, 2023 9:47 am

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

Post by kuinashi101 »

It should be called CSP( comma separated pair ) instead of CSV I suggest, and I wonder if any application like Microsoft Excel, MacOS Number.. support this CSP format output.

It's quite a handy module which deal the sort of innovative format that provide another option rather than CSV and Json. I found it's quite convenient for handling simple structure data like config files and leaderboard. thanks for the nice work!
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post by ivan »

Hello, this is a good attempt but the library needs work in terms of the required dependencies and the code.
I recommend modifying the csvfile:parse function to accept strings. Right now this function works using io.open while other parts of the code use love.filesystem. You need to remove the love.filesystem dependency and this project will become a "general purpose" library.
Some of the code could be simplified, for example:

Code: Select all

s:gmatch(string.format("%s%s%s","([%w_-]+)",sep,"([%w_-]+)"))
Could become:

Code: Select all

s:gmach("([%w_-]+)"..sep.."([%w_-]+)")
Please note that this code would break if "sep" contains any of the Lua pattern-matching special characters.

Also, you do not need the following lines:

Code: Select all

else
        return nil;
Lastly, I don't think table.join or table.merge is a part of the standard Lua table. module
This library is supposed to parse a few strings, so it is silly to require Love2D and some mysterious module (iTable) in order to get it working.
User avatar
dusoft
Party member
Posts: 482
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post by dusoft »

It's a good exercise to develop your own CSV handling library, but there are plenty of *tested* CSV libraries on Github for Lua.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 12 guests