csv to nested list?

Q&A's, tips, howto's

csv to nested list?

Postby tomtoo » Sun Oct 29, 2017 1:29 pm

Hi guys,

I would like to use lookup and/or assoc to generate html tables. The source data will come from 4 different csv (maybe tsv) files.

This bit from the manual seems almost perfect:

Code: Select all
(set 'persons '(
      (id001 (name "Anne") (address (country "USA") (city "New York")))
      (id002 (name "Jean") (address (country "France") (city "Paris")))
      ))


Or this:
Code: Select all
(set 'persons '(
    ("John Doe" 35 "M" 12.34)
    ("Mickey Mouse" 65 "N" 12345678)


I'm having trouble working out how to populate my list with elements from the different sources.

Help?

It seems like this is a common enough scenario, but I can't quite get it without things getting really ugly.

Thanks :-)
tomtoo
 
Posts: 46
Joined: Wed Oct 28, 2009 10:00 pm

Re: csv to nested list?

Postby ralph.ronnquist » Mon Oct 30, 2017 4:59 am

Doing it properly might require a proper CSV parser, like
https://github.com/kanendosei/artful-newlisp/blob/master/csv.lsp

I must admit I haven't tried it myself, but it looks fine at a glance.

Load that file, then it's simply a matter of
Code: Select all
(CSV:parse-file csvfile)
if you're using comma as delimiter and double-quote as quote character.
ralph.ronnquist
 
Posts: 178
Joined: Mon Jun 02, 2014 1:40 am
Location: Melbourne, Australia


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 2 guests