diff --git a/AUTHORS b/AUTHORS index 8320700..5e825e2 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,2 +1,3 @@ This csv reader is originally wrote by Andy Wingo -And modified/maintaining by Nala Ginrut +Modified and maintained by Nala Ginrut +Further modified for use by Brady McDonough diff --git a/README.md b/README.md index e44c4b8..7599dd2 100644 --- a/README.md +++ b/README.md @@ -1,57 +1,75 @@ guile-csv ========= -Guile csv reader - -## USAGE +A csv reader for Guile. Forked from [Nala Ginrut's][og-repo] repository of the +same name. I forked simply because I plan on using this code (or some +derivative) and as my needs change so too will the interface. Pushing breaking +changes to a 5 year old repository isn't exactly my idea of a good time. ### install `$make` + `$sudo make install` +## General usage + +### reading + +First get yourself a port out of whatever csv you want to process: -### read csv ```scheme (use-modules (csv csv)) -(define my-csv-reader (make-csv-reader #:\,)) -(call-with-input-file "file.csv" my-csv-reader) +(define $port (open-input-string "a,b\n1,2\n3,4")) ``` -### csv->xml +Then, you can process it into a form of your choice between xml and sxml. + ```scheme -(call-with-input-file "file.csv" csv->xml) +(csv->xml $port) ``` -and result could be: -```xml - - aaa - 11 - aaa@aaa.com - - - bbb - 12 - bbb@bbb.com - +Output: +``` +"1234" ``` -### sxml->csv or csv-write to output a csv format file +If you plan on working with the parsed output further in scheme you'll probably +want to use `(csv->sxml)` with the optional argument `#:record-sym` like +so: ```scheme -(call-with-output-file "file.csv" - (lambda (port) - (sxml->csv - '((name age email) ("aaa" "11" "aaa@aaa.com") ("bbb" "12" "bbb@bbb.com")) - port))) +(csv->sxml $port + #:record-sym (lambda n "rec")) ``` -and file.csv would be: -```csv -name,age,email -aaa,11,aaa@aaa.com -bbb,12,bbb@bbb.com +Output: +```sheme +(*TOP* (rec ((a "1") (b "2"))) (rec ((a "3") (b "4")))) ``` -Enjoy! +`#:record-sym` expects a procedure and is called with a numeric argument to +generate a string. Ignoring the argument and using a single symbol for all +records as seen in the example above could be useful if you plan on +manipulating the resulting tree using something like `(sxml transform)`. + +### writing + +While there are facilities to write csv strings and files provided user be +warned: they don't quite do what they say on the tin. Calling `sxml->csv` +with the output from `csv->sxml` for example will result in an error. +The "sxml" expected by this function is what I would venture to call "scsv". +```scheme +`((a b c)("1" "2" "3")("4" "5" "6")) +``` +Note the double quotes surrounding each data point, the algorithm currently +does not attempt to make any conversions and `(string-join)` will complain +accordingly. + +I don't have any need for converting back into csv and have no personal plans +on improving this situation (just in case you were preparing to hold your +breath). If anyone reading this does want such functionality and has a +description of how they think the function ought to act please do raise an +issue. + +[og-repo]:(https://gitlab.com/NalaGinrut/guile-csv)